Is it a way to prevent gtest (C++) from catching exceptions and segmentation faults?

Viewed 1230

Looks like Google Test (gtest) framework for C++ somehow catches all failed assertions, exceptions and segmentation faults. The test is marked as failed but the test suite itself recovers and runs the tests that follow after.

This looks kind of cool, just that the diagnostic output that the gtest framework prints to console is very limited. CLion IDE would provide much more information, including the full stack trace and variables. Is it an easy way to turn the gtest "crash recovery" mechanism off?

2 Answers

I'm quite sure that GTest cannot "catch" segmentation fault - it's OS interrupting your program, not program interrupting itself.

To disable catching failures, you can do the following (from documentation)

  • Disable catching exceptions
    Set environment variable GTEST_CATCH_EXCEPTIONS to zero or run your tests with --gtest_catch_exceptions=0 flag
  • Make GTest put a breakpoint on failure (when running under debugger) Set environment variable GTEST_BREAK_ON_FAILURE to non-zero value or run your tests with --gtest_break_on_failure flag

The second option will drop you directly into debugger interactive mode on any failure, not only escaped exception.

You can run your test binary with the option --gtest_catch_exceptions=0. This will cause the test to crash on exception

Do not report exceptions as test failures. Instead, allow them to crash the program or throw a pop-up (on Windows).

Alternatively you can set this environmental var GTEST_CATCH_EXCEPTIONS to 0;

These features are mentioned in gtest docs

Related