如何使用Google Test捕获细分错误? [英] How to catch segmentation fault with Google Test?

查看:189
本文介绍了如何使用Google Test捕获细分错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I test that a function won't produce a segmentation fault?

这就是我现在所知道的,我可以做:

Here what I know right now, I can do:

EXPECT_DEATH(foo(nullParameter))

在该功能中,产生分段错误,这是我要使之失败的行为.上面的代码片段将使测试通过,因为这是预期的结果,即过程的终止.

In side the function, a segmentation fault is produce which is the behavior that I want to make fail. The snippet above will make the test pass because that is what is expected, the death of the process.

现在,如何使它失败?

推荐答案

这是一个函数,如果传递了null指针参数,则该函数会出现段错误 不是:

Here's a function that will segfault if passed a null pointer argument and otherwise not:

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

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

And here is a googletest program that tests that behaviour:

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)是没有意义的测试, 因为我无法想到要保证的任何情况 我本人认为,由于对某个 我写的功能.

As far as I can imagine, TEST(test_deref_1,will_segfault) is a pointless test, because I cannot think of any circumstances in which I would want to assure myself that a program will segfault as a result of making a certain call to a function I have written.

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

TEST(test_dref_2,will_not_segfault) is possibly a useful kind of test. In effect, it is a test that the program:

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

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

will terminate by exit(0) rather than in any premature abnormal way. A better name for this test would probably be TEST(test_dref,does_not_crash), or similar.

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

It is a possibly useful kind of test because there could be a significant risk of it failing, if defref was some sufficiently complicated code, and the test suite could report that failure without crashing itself. We can force a failure by rewriting it:

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

然后测试的测试报告是:

and then test test report is:

$ ./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 的文档 了解这些宏.

See the documentation of {ASSERT|EXPECT}_EXIT to understand these macros.

这篇关于如何使用Google Test捕获细分错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆