EXPECT_DEATH的逆是什么? [英] What is the inverse of EXPECT_DEATH?
问题描述
对于Google测试,我们假设以下代码
#include< iostream>使用命名空间std;使用MyFunc = void(*)(void);无效foo_robust(MyFunc f){if(f!= nullptr)(* f)();}无效foo_not_robust(MyFunc f){(* f)();}void print(void){cout<<"hello world"<<恩德尔}int main(){foo(& print);//作品foo(nullptr);//运行时错误?返回0;}
使用Google测试时,
我可以做到:
TEST(TestAssertDeath,死亡){EXPECT_DEATH(foo(& print));//将返回FAILED,因为不会死.EXPECT_DEATH(foo(nullptr));//如果foo健壮,将返回FAILED,否则返回OK(UB:它甚至可能返回FAILED)}
我想做
TEST(TestAssertDeath,No_Death){EXPECT_NO_DEATH(foo(& print));//将返回OK,因为不会死.EXPECT_NO_DEATH(foo(nullptr));//如果foo是健壮的,则返回OK,否则返回FAILED或DEATH(UB:它甚至可能返回OK)}
是否有任何Google宏执行"EXPECT_NO_DEATH"的工作?我尝试了EXPECT_NO_FATAL_FAILURE,但是它崩溃了,好像我什么都没放.我想做:
TEST(TestAssertDeath,No_Death){EXPECT_NO_FATAL_FAILURE(foo(& print));//将返回OK,因为不会死.EXPECT_NO_FATAL_FAILURE(foo(nullptr));//将崩溃(UB:甚至可能返回OK)}
但我不希望测试野营活动停止.
Google测试当前给我以下错误.
[RUN] MyClass.MyUnitTestA[ 好的 ][运行] MyClass.MyUnitTestB[ 好的 ][运行] MyClass.MyUnitTestCmingw32-make [3]:*** [CMakeFiles/myproject-coverage]错误-1073741819mingw32-make [2]:*** [CMakeFiles/myproject-coverage.dir/all]错误2mingw32-make [1]:*** [CMakeFiles/myproject-coverage.dir/rule]错误2mingw32-make:*** [myproject-coverage]错误2
因为这会停止其他测试的运行,所以如果代码不够健壮,我想做如下的事情
[RUN] MyClass.MyUnitTestA[ 好的 ][运行] MyClass.MyUnitTestB[ 好的 ][运行] MyClass.MyUnitTestC[死亡]或[失败][运行] MyClass.MyUnitTestD[ 好的 ]
,如果代码很健壮,还可以这样做:
[RUN] MyClass.MyUnitTestA[ 好的 ][运行] MyClass.MyUnitTestB[ 好的 ][运行] MyClass.MyUnitTestC[ 好的 ][运行] MyClass.MyUnitTestD[ 好的 ]
重要说明::我知道foo_not_robust(nullptr)行是UB,不会自动以崩溃结尾,但是如果确实如此,我希望跳过这一行并将其标记为失败
要使单元测试更可靠地防止崩溃,您可以尝试在子进程中运行每个测试,并使用父进程来监视子进程的成功,失败或失败.崩溃.
但是有一个简单的方法,实际上您可以做一些EXPECT_NO_DEATH
来自Google测试文档:
TEST(MyDeathTest,NormalExit){EXPECT_EXIT(NormalExit(),:: testing :: ExitedWithCode(0),成功");}
您可以使用两个语句(statement1,statement2)
和 statement1 =语句
和 statement2 = exit(0)
>
它提供了以下两个自定义宏:
#define EXPECT_CRASH(statement)\EXPECT_EXIT((statement,exit(0)),:: testing :: KilledBySignal(SIGSEGV),.*")
#define EXPECT_NO_CRASH(statement)\EXPECT_EXIT((statement,exit(0)),:: testing :: ExitedWithCode(0),.*")
EXPECT_CRASH()
等效于 EXPECT_DEATH()
EXPECT_NO_CRASH()
等同于请求的 EXPECT_NO_DEATH()
请注意, :: testing :: KilledBySignal(signal_number)
在Windows上不可用.作为Windows的解决方法,您可以做的只是定义:
#define EXPECT_CRASH(statement)\EXPECT_DEATH(声明,.*")
给出以下消息:
[RUN] MyClass.MyUnitTestA[ 好的 ][运行] MyClass.MyUnitTestB[ 好的 ][运行] MyClass.MyUnitTestC死亡测试:(foo(),exit(0))结果:已死亡,但未出现预期的退出代码:以退出状态-1073741819退出实际味精:[ 死亡 ][运行] MyClass.MyUnitTestD[ 好的 ]
With google tests, let's assume the following code
#include <iostream>
using namespace std;
using MyFunc = void (*)(void);
void foo_robust(MyFunc f) { if(f != nullptr) (*f)(); }
void foo_not_robust(MyFunc f) { (*f)(); }
void print(void) { cout << "hello world" << endl; }
int main()
{
foo(&print); //works
foo(nullptr); //runtime error ?
return 0;
}
When using google test,
I can do:
TEST(TestAssertDeath, Death)
{
EXPECT_DEATH(foo(&print)); //will return FAILED, because does not die.
EXPECT_DEATH(foo(nullptr)); //will return FAILED if foo robust, OK otherwise (UB: it might even return FAILED)
}
I want to do:
TEST(TestAssertDeath, No_Death)
{
EXPECT_NO_DEATH(foo(&print)); //will return OK, because does not die.
EXPECT_NO_DEATH(foo(nullptr)); // will return OK, if foo is robust, FAILED or DEATH otherwise (UB: it might even return OK)
}
Is there any google macro that does "EXPECT_NO_DEATH" 's job? I tried EXPECT_NO_FATAL_FAILURE but it crashes as if I put nothing. I want to do:
TEST(TestAssertDeath, No_Death)
{
EXPECT_NO_FATAL_FAILURE(foo(&print)); //will return OK, because does not die.
EXPECT_NO_FATAL_FAILURE(foo(nullptr)); // will crash (UB: might even return OK)
}
But I don't want the test campaing to be stopped.
Google test gives me currently the following error.
[ RUN ] MyClass.MyUnitTestA
[ OK ]
[ RUN ] MyClass.MyUnitTestB
[ OK ]
[ RUN ] MyClass.MyUnitTestC
mingw32-make[3]: *** [CMakeFiles/myproject-coverage] Error -1073741819
mingw32-make[2]: *** [CMakeFiles/myproject-coverage.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles/myproject-coverage.dir/rule] Error 2
mingw32-make: *** [myproject-coverage] Error 2
Because this stops the other tests from running, I would like something as the following if the code is not robust
[ RUN ] MyClass.MyUnitTestA
[ OK ]
[ RUN ] MyClass.MyUnitTestB
[ OK ]
[ RUN ] MyClass.MyUnitTestC
[ DEATH ] or [ FAILED ]
[ RUN ] MyClass.MyUnitTestD
[ OK ]
and this if the code is robust:
[ RUN ] MyClass.MyUnitTestA
[ OK ]
[ RUN ] MyClass.MyUnitTestB
[ OK ]
[ RUN ] MyClass.MyUnitTestC
[ OK ]
[ RUN ] MyClass.MyUnitTestD
[ OK ]
Important note: I know that the line foo_not_robust(nullptr) is UB and will not ends up in a crash automatically, but if it does, I want this line to be skipped and marked as failed.
To make unit tests robust against crashing you could try to run each test in a child process, with a parent process that monitors the child process for success, failure or crash.
But there is an easy way, in fact you can do something as EXPECT_NO_DEATH
From google test doc:
TEST(MyDeathTest, NormalExit) { EXPECT_EXIT(NormalExit(), ::testing::ExitedWithCode(0), "Success"); }
You can hack it by using two statements (statement1,statement2)
with statement1 = statement
and statement2 = exit(0)
It gives the two following custom macros:
# define EXPECT_CRASH(statement) \
EXPECT_EXIT((statement,exit(0)),::testing::KilledBySignal(SIGSEGV),".*")
# define EXPECT_NO_CRASH(statement) \
EXPECT_EXIT((statement,exit(0)),::testing::ExitedWithCode(0),".*")
EXPECT_CRASH()
is equivalent to EXPECT_DEATH()
EXPECT_NO_CRASH()
is equivalent the requested EXPECT_NO_DEATH()
Note that ::testing::KilledBySignal(signal_number)
is not available on Windows.
What you could do as a workaround for Windows is just defining:
# define EXPECT_CRASH(statement) \
EXPECT_DEATH(statement,".*")
gives the following message:
[ RUN ] MyClass.MyUnitTestA
[ OK ]
[ RUN ] MyClass.MyUnitTestB
[ OK ]
[ RUN ] MyClass.MyUnitTestC
Death test: (foo(),exit(0))
Result: died but not with expected exit code:
Exited with exit status -1073741819
Actual msg:
[ DEATH ]
[ RUN ] MyClass.MyUnitTestD
[ OK ]
这篇关于EXPECT_DEATH的逆是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!