如何利用Google Test捕获分段错误?

Dan*_*tos 6 c++ unit-testing googletest

如何测试函数不会产生分段错误?

这就是我现在所知道的,我能做到:

EXPECT_DEATH(foo(nullParameter))
Run Code Online (Sandbox Code Playgroud)

在函数的一侧,产生一个分段错误,这是我想要失败的行为.上面的代码段将使测试通过,因为这是预期的,过程的死亡.

现在,我怎么能让它失败?

Mik*_*han 10

这是一个函数,如果传递一个空指针参数将会段错误,否则不会:

int deref(int * pint)
{
    return *pint;
}
Run Code Online (Sandbox Code Playgroud)

这是一个测试该行为的googletest程序:

main.cpp中

#include <gtest/gtest.h>

int deref(int * pint)
{
    return *pint;
}


TEST(test_deref_1,will_segfault)
{
    ASSERT_EXIT((deref(nullptr),exit(0)),::testing::KilledBySignal(SIGSEGV),".*");
}


TEST(test_dref_2,will_not_segfault)
{
    int i = 42;
    ASSERT_EXIT((deref(&i),exit(0)),::testing::ExitedWithCode(0),".*");
}


int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)

编译和链接:

$ g++ -Wall -Wextra -pedantic -o tester main.cpp -pthread -lgtest
Run Code Online (Sandbox Code Playgroud)

跑:

$ ./tester 
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from test_deref_1
[ RUN      ] test_deref_1.will_segfault
[       OK ] test_deref_1.will_segfault (168 ms)
[----------] 1 test from test_deref_1 (168 ms total)

[----------] 1 test from test_dref_2
[ RUN      ] test_dref_2.will_not_segfault
[       OK ] test_dref_2.will_not_segfault (1 ms)
[----------] 1 test from test_dref_2 (1 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (169 ms total)
[  PASSED  ] 2 tests.
Run Code Online (Sandbox Code Playgroud)

据我所知,这TEST(test_deref_1,will_segfault)是一个毫无意义的测试,因为我无法想到在任何情况下我都要向自己保证一个程序会因为对我编写的函数进行某些调用而产生段错误.

TEST(test_dref_2,will_not_segfault)可能是一种有用的测试.实际上,它是一个测试程序:

int main()
{
    int i = 42;
    defref(&i);
    exit(0);
}
Run Code Online (Sandbox Code Playgroud)

将以exit(0)任何过早的异常方式终止而不是.这个测试的更好的名称可能是TEST(test_dref,does_not_crash)或类似的.

这是一种可能有用的测试,因为它可能存在很大的失败风险 ,如果defref是一些足够复杂的代码,并且测试套件可以报告该失败而不会崩溃.我们可以通过重写来强制失败:

TEST(test_dref_2,will_not_segfault)
{
    ASSERT_EXIT((deref(nullptr),exit(0)),::testing::ExitedWithCode(0),".*");
}
Run Code Online (Sandbox Code Playgroud)

然后测试测试报告是:

$ ./tester
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from test_deref_1
[ RUN      ] test_deref_1.will_segfault
[       OK ] test_deref_1.will_segfault (147 ms)
[----------] 1 test from test_deref_1 (147 ms total)

[----------] 1 test from test_dref_2
[ RUN      ] test_dref_2.will_not_segfault
main.cpp:25: Failure
Death test: (deref(nullptr),exit(0))
    Result: died but not with expected exit code:
            Terminated by signal 11 (core dumped)
Actual msg:
[  DEATH   ] 
[  FAILED  ] test_dref_2.will_not_segfault (90 ms)
[----------] 1 test from test_dref_2 (90 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (237 ms total)
[  PASSED  ] 1 test.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] test_dref_2.will_not_segfault

 1 FAILED TEST
Run Code Online (Sandbox Code Playgroud)

请参阅文档{ASSERT|EXPECT}_EXIT 以了解这些宏.