How to use googletest Failures into Break-Points
Asked Answered
F

2

29

I recently discovered the Failures into Break-Points - option from googletest using the command line option gtest_break_on_failure or by defining the GTEST_BREAK_ON_FAILURE environment variable.

I gave it a try using gtest_break_on_failure. From command line, I saw no effect (to be honest I had the glimpse of hope that VS2010 would be registered as debugger and somehow magically would pop up and point to the error source).

Using it in the VS environment as command line argument a failed assertion triggered a break but the call stack did not include the test method that caused the failure. I found the work around to step (F10) until I reached my test code, but that does not really seem to be convenient.

Is it somehow possible to use the option from command line ?

Has anybody a recommendation how to get the correct call stack in the environment?

Florance answered 3/4, 2013 at 7:48 Comment(0)
J
43

From VS, you can add --gtest_break_on_failure to the Command Args in the target's Property Pages, then just run the exe without stepping over.

Property Pages showing Command Arguments

From the command line, you should be able to run the Debug executable with the flags --gtest_break_on_failure --gtest_catch_exceptions=0 and this should allow you to break into the MSVC debugger when the test fails.

Jessie answered 3/4, 2013 at 20:49 Comment(3)
Man, I can't believe it! the --gtest_catch_exceptions=0 really worked from the command line. Thank you.Florance
I had to add both when running in my VS environment - but it may be how we have our project set up. Still it works and thanks.Arrowy
IMO, gtest should enable these if IsDebuggerPresentGarver
G
1

Here's how to enable break_on_failure automatically when running tests under debugger. googletest comes with its own main entry point in gtest_main.cc, to override default behavior either modify it, or add your own main to your test project.

Here's the change I added:

#include "gtest/gtest.h"
#include <windows.h>

GTEST_API_ int main(int argc, char** argv)
{
#ifdef _WIN32
    if (IsDebuggerPresent())
        testing:: GTEST_FLAG(break_on_failure) = true;
#endif
    //printf("Running main() from %s\n", __FILE__);
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

Specifically, that part ifdef'ed for _WIN32 automatically sets break_on_failure GTEST_FLAG if IsDebuggerPresent.

Garver answered 7/3 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.