为什么Google Test会出现分段错误?

lia*_*ang 3 c++ debugging unit-testing googletest

我是Google Test的新手,我正在玩提供的示例.我的问题是,当我引入故障并设置GTEST_BREAK_ON_FAILURE=1(或使用命令行选项)时,GTest将会出现段错误.

我正在考虑这个例子.如果我在任何测试中插入这样的东西,我将开始得到段错误:

EXPECT_EQ(8, 2*3);

只是重申一下,那只有在我也定下来的时候GTEST_BREAK_ON_FAILURE=1.我从命令行运行,也运行gdb.如果未设置该环境变量,则会报告错误但不会出现段错误.

什么可能导致这个/我做错了什么的任何线索?我一直在寻找类似的问题,但我还没有碰到任何东西.

仅供参考我在64位CrunchBang Linux 11"Waldorf"上运行Google Test 1.7.0版.

编辑代码示例:

// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
  EXPECT_EQ(1, Factorial(1));
  EXPECT_EQ(2, Factorial(2));
  EXPECT_EQ(6, Factorial(3));
  EXPECT_EQ(40320, Factorial(8));
}
Run Code Online (Sandbox Code Playgroud)

调试器输出:

(gdb) run
Starting program: /home/yourfavoriteprotein/bin/cpp_unit_test_frameworks/gtest-1.7.0/samples/mytest 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Running main() from test_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
sample1_unittest.cc:112: Failure
Value of: 2*3
  Actual: 6
Expected: 8

Program received signal SIGSEGV, Segmentation fault.
0x0000000000413427 in testing::UnitTest::AddTestPartResult(testing::TestPartResult::Type,         char const*, int, std::string const&, std::string const&) ()
(gdb) quit
Run Code Online (Sandbox Code Playgroud)

Jos*_*ley 7

GTEST_BREAK_ON_FAILURE=1表示如果测试失败,Google Test 会将您带入调试器.

事实上,一种简单,可移植的方式让您进入调试器是触发段错误.

换句话说,这种行为是设计的; Google Test故意触发段错误以使调试器运行.(见这里在谷歌测试代码.)

  • @yourfavoriteprotein是的,这是设计的.它不会通过启动调试器"让你进入调试器".它只是停止程序(通过segfaulting),因此如果您在调试器中运行测试,则可以检查堆栈跟踪并对失败的位置进行基本调试. (2认同)