【发布时间】:2019-05-03 03:14:30
【问题描述】:
我正在用 C++ 编写一个非常简单的单元测试框架,它使用 try-catch 语句来分析变量。在此示例中,我有一个名为 UnitTest 的类,其中一个公共成员函数和一个私有成员函数分别名为 scalars_are_equal() 和 is_equal()。用户必须将str 传递给公共函数,其中包含一个带有测试名称的字符串。用户还必须将value1 和value2 传递给包含两个要相互比较的标量值的公共函数。公共函数使用try 语句并将数据传递给私有函数,在该私有函数中评估两个变量以查看它们是否匹配。如果值匹配,则返回调用函数,在该函数中将一条消息打印到屏幕上,让用户知道测试通过。如果值不相等,则私有函数应抛出分配给字符串msg 的异常,而公共函数应捕获此异常。课程附在下面。这些函数被编写为模板函数,因此用户可以选择比较整数、浮点数和双精度数,即使浮点运算可能意味着一个数字的两个版本并不完全相同。
class UnitTest
{
public:
template <class type1, class type2>
void scalars_are_equal(std::string str, const type1 &value1, const type2 &value2);
private:
template <class type1, class type2>
void is_equal(const type1 &value1, const type2 &value2, std::string str);
};
// ================================================================
// ================================================================
// UnitTest PUBLIC member functions
template <class type1, class type2>
void UnitTest::scalars_are_equal(std::string str, const type1 &value1,
const type2 &value2)
{
unsigned long remain;
remain = 60 - str.length();
try {
is_equal(value1, value2, str);
std::cout << str + std::string(remain, '.') +
std::string("PASSED") << std::endl;
}
catch (const char* msg) {
std::cout << msg << std::endl;
}
}
// ----------------------------------------------------------------
template <class type1, class type2>
void UnitTest::is_equal(const type1 &value1, const type2 &value2,
std::string str)
{
if (value1 != value2) {
unsigned long remain;
remain = 60 - str.length();
std::string msg = str + std::string(remain, '.') + " FAILED";
throw msg;
}
}
在这种情况下,主程序看起来像;
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include "unit_test.hpp"
int main(int argc, const char * argv[]) {
UnitTest q;
{ // Test to see if code catches unequal scalars
float a, b;
a = 20.0;
b = 30.0;
std::string c ("Scalars_Unequal_Test");
q.scalars_are_equal(c, a, b);
}
由于我不明白的原因,函数scalars_are_equal() 中的catch 语句没有捕获is_equal() 函数。起初我以为可能是因为函数抛出了std::string,但是当我将catch 语句从const char 更改为std::string 时,并没有什么不同。有谁知道为什么这没有捕捉到异常?
【问题讨论】:
-
你真的不需要show the issue的所有代码。提示:
std::string不是const char *。 -
但是当我将 catch 语句从 const char 更改为 std::string 时,它没有任何区别 -- 请发布此尝试。
-
能够重现
const char *,但这是预期的行为。无法重现std::string版本。我必须对代码进行编译以使其编译的更改可能已经改变了一些东西。我们可以得到minimal reproducible example 自己试用吗? -
在一种不相关的注释上:这并不是 try and catch 的真正用途。 Try and catch 用于捕获错误并从错误中恢复,而不是来回传递数据,这就是您正在做的事情。
-
@Chipster 我想说为单元测试失败抛出异常是很合理的,这是许多单元测试框架的工作方式