gtest: where to put gdb breakpoint
Asked Answered
B

3

6

I am using various xunit tests for years (starting from cppunit in early 2000s). In all cases it was very easy to set a break point on failure: there was a function that indicated detected failure:

b 'atf::tests::tc::fail(std::string const&)' command up 1 end

It seems that gtest is quite different, what is the established practice of doing the same with gtest?

Blondie answered 18/3, 2019 at 22:11 Comment(0)
R
6

what is the established practice of doing the same with gtest?

Reading gtest.cc, the closest I see is --gunit_break_on_failure, which should cause the code to execute INT3 trap on x86/Linux, and to call DebugBreak on Windows.

Update: the flag appears to have been renamed to --gtest_break_on_failure in latest public releases.

Ratline answered 19/3, 2019 at 2:1 Comment(2)
It kinda sorta works - but so far I do not see a way to continue after non-fatal failure and associate debugger commands (e.g. 'up 2'). It will be nice to get something friendlier.Blondie
@zzz777 If you are on "Linux/x86", you can continue from int3 with GDB signal 0 command.Ratline
J
5

If you need to break at the start of the test to observe something, first get the symbol names present in the executable, and grep for the test name of interest, e.g.:

nm -C myclass_test | grep MyTest0

if you want to break at:

TEST(MainTest, MyTest0) {
    EXPECT_EQ(1, 1);
}

Of the results of that grep, the most promising one seemed to be:

0000000000407c64 T MainTest_MyTest0_Test::TestBody()

and so:

gdb myclass_test

and:

b MainTest_MyTest0_Test::TestBody
r

and then this leaves me at the start of the desired test.

Tested with this setup at revision 2.

Jews answered 25/11, 2020 at 19:7 Comment(0)
C
0

You can use break test.cpp:42 to break on line 42 of the unit test. That way you don't have to look up the generated symbol names of the tests.

Conard answered 22/1 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.