【问题标题】:How to catch segmentation fault with Google Test?如何使用 Google Test 捕捉分段错误?
【发布时间】:2020-03-25 07:16:49
【问题描述】:

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

以下是我现在所知道的,我可以做到:

EXPECT_DEATH(foo(nullParameter))

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

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

【问题讨论】:

  • 我猜除了不会死吗?你想要什么,测试人员遗漏的段错误?
  • 一旦发生段错误就太晚了。防止被公共汽车撞到比复活死者更容易。
  • 这背后的想法是我知道该函数导致了段错误,并且将有一个补丁来解决这个问题。现在我的目标是在创建路径之前进行失败的测试。

标签: c++ unit-testing googletest


【解决方案1】:

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

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

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

ma​​in.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

the documentation of {ASSERT|EXPECT}_EXIT 了解这些宏。

【讨论】:

  • 文档还非常强调not using underscores in test names。你或许应该重构你的代码。
  • @Mike Kinghan 但是如果发生故障,仍然会创建核心文件,对吧?
【解决方案2】:

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

【讨论】:

  • 我想要的是测试段错误不会发生。比如我想说什么EXPECT_NO_DEATH,表示代码执行成功。
猜你喜欢
  • 2017-09-11
  • 1970-01-01
  • 2010-12-06
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
  • 2017-09-22
相关资源
最近更新 更多