Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在调试模式下绕过单元测试中的断言?_C++_Unit Testing_Assert_Catch Unit Test - Fatal编程技术网

C++ 如何在调试模式下绕过单元测试中的断言?

C++ 如何在调试模式下绕过单元测试中的断言?,c++,unit-testing,assert,catch-unit-test,C++,Unit Testing,Assert,Catch Unit Test,在测试用例中,我想测试一个函数,该函数在调试模式下为无效输入生成断言。不幸的是,这使测试运行程序停止。有没有办法绕过这个断言,让测试运行程序继续运行 以下是我的测试用例: SCENARIO("Simple test case", "[tag]") { GIVEN("some object") { MyObject myobject; WHEN("object is initialized with invalid data") {

在测试用例中,我想测试一个函数,该函数在调试模式下为无效输入生成断言。不幸的是,这使测试运行程序停止。有没有办法绕过这个断言,让测试运行程序继续运行

以下是我的测试用例:

 SCENARIO("Simple test case", "[tag]") {
    GIVEN("some object") {
        MyObject myobject;

        WHEN("object is initialized with invalid data") {
            // method init generates an assertion when parameters are invalid
            bool result = myObject.init(nullptr, nullptr, nullptr, nullptr);
            REQUIRE(false == result);

            THEN("data processing can't be started") {
            }
        }
    }
}

通常,
assert
是一个宏

#define assert(e) ((void) ((e) \
  ? 0 
  : (void)printf ("%s:%u: failed assertion `%s'\n",  __FILE__, __LINE__, #e),
    abort(), // <-- this aborts you program after printf's above
    0        
)
定义断言(e)(无效)(e)\ ? 0 :(void)printf(“%s:%u:failed assertion`%s'\n”、\uuuuuu文件、\uuuuuu行、\e),
abort(),//断言用于防止在正确的程序中永远不会发生的情况。为什么要进行单元测试?如果在正确的程序中可能发生无效参数(例如,来自格式错误的用户输入),则应使用异常而不是断言。@Quentin实际上它是一个外部库(由Matlab code generator生成)这是我想测试的。我们为生成的代码生成了一个包装器,但现在不知道库是否能够处理现实生活中可能发生的无效数据,因为这些数据来自输入文件。我同意在我们的情况下,使用异常可能是比断言更好的解决方案。这与