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

问题描述 投票:0回答:3

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

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

EXPECT_DEATH(foo(nullParameter))

在函数内部,会产生分段错误,这是我想要失败的行为。上面的代码片段将使测试通过,因为这是预期的,进程的死亡。

现在,我怎样才能让它失败?

c++ unit-testing googletest
3个回答
23
投票

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

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

这是一个测试该行为的 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();
}

编译并链接:

$ g++ -Wall -Wextra -pedantic -o tester main.cpp -pthread -lgtest

运行:

$ ./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.

据我想象,

TEST(test_deref_1,will_segfault)
是一个毫无意义的测试, 因为我想不出在什么情况下我想保证 我自己认为程序会由于对某个调用进行特定调用而出现段错误 我写的函数。

TEST(test_dref_2,will_not_segfault)
可能是一种有用的测试。有效, 这是一个测试,该程序:

int main()
{
    int i = 42;
    defref(&i);
    exit(0);
}

将由

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),".*");
}

然后测试测试报告为:

$ ./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

请参阅

{ASSERT|EXPECT}_EXIT
的文档 了解这些宏。


0
投票

崩溃的测试已经失败(大概你不希望代码中的任何出现段错误)。与任何其他测试一样,只需测试您期望的行为即可。


0
投票

当被测试的程序出现段错误时,GoogleTest 会崩溃,而不会产生有用的测试报告,因此一般来说每个测试都需要在子进程中运行。

一种方法是将所有测试包装到 exit(0) 和 ASSERT_EXIT,如最上面的答案中所述。

但是,更方便的方法是使用 gtest-parallel。使用

gtest-parallel --serialize_test_cases --worker=1
,它只是将
gtest
包装在子进程中,从而解决了问题,而无需对测试进行任何更改。它的选项与
gtest
基本相同。

© www.soinside.com 2019 - 2024. All rights reserved.