gpt4 book ai didi

c++ - EXPECT_DEATH 的倒数是什么?

转载 作者:行者123 更新时间:2023-11-28 04:12:51 25 4
gpt4 key购买 nike

通过谷歌测试,让我们假设以下代码

#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;
}

使用google测试时,

我能做到:

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)
}

我想做的事:

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)
}

是否有任何谷歌宏可以完成“EXPECT_NO_DEATH”的工作?我试过 EXPECT_NO_FATAL_FAILURE 但它崩溃了,就好像我什么也没放一样。我想做的:

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)
}

但我不希望测试事件停止。

Google 测试目前给我以下错误。

[ 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

因为这会阻止其他测试运行,所以如果代码不健壮,我想要如下内容

[ RUN      ] MyClass.MyUnitTestA
[ OK ]
[ RUN ] MyClass.MyUnitTestB
[ OK ]
[ RUN ] MyClass.MyUnitTestC
[ DEATH ] or [ FAILED ]
[ RUN ] MyClass.MyUnitTestD
[ OK ]

如果代码是健壮的:

[ RUN      ] MyClass.MyUnitTestA
[ OK ]
[ RUN ] MyClass.MyUnitTestB
[ OK ]
[ RUN ] MyClass.MyUnitTestC
[ OK ]
[ RUN ] MyClass.MyUnitTestD
[ OK ]

重要说明:我知道 foo_not_robust(nullptr) 行是 UB,不会自动崩溃,但如果发生崩溃,我希望跳过此行并将其标记为失败.

最佳答案

为了使单元测试对崩溃具有鲁棒性,您可以尝试在子进程中运行每个测试,父进程监控子进程的成功、失败或崩溃。

但是有一个简单的方法,实际上你可以做一些EXPECT_NO_DEATH

来自谷歌测试文档:

TEST(MyDeathTest, NormalExit) {
EXPECT_EXIT(NormalExit(), ::testing::ExitedWithCode(0), "Success");
}

您可以通过使用两个语句 (statement1,statement2)statement1 = statementstatement2 = 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(statement,".*")

给出以下信息:

[ 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 ]

关于c++ - EXPECT_DEATH 的倒数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57312559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com