Search code examples
c++unit-testinggoogletest

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


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?


Solution

  • 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.