c++unit-testingboostboost-testgoogle-breakpad

How to run Boost Test and produce Minidumps?


Our project uses Boost.Test for our units tests. We would like to get minidumps when unexpected exceptions occur during our test cases as well, so we've started integrating Google Breakpad to write the minidumps.

It looks like Boost.Test is intercepting all thrown exceptions from user tests - I'm assuming because Boost test cases wrap each function with a try / catch and the unit test simply fails if an unexpected exception is thrown. This prevents the Breakpad exception handler from firing and writing minidumps.

Is it possible to have Boost.Test not just catch and fail on unexpected exceptions in unit tests? And to instead let the exceptions go unhandled (or rethrow) so Breakpad or another exception handler can be triggered to write a minidump?


Solution

  • I tried a few different approaches but the following solution provides the best result. Defining a macro to wrap the BOOST_AUTO_TEST_CASE macro, and surrounding the calling code with SEH __try/__except and piping the exception data into Breakpad.

    #define CUSTOM_AUTO_TEST_CASE( test_name )                                                                  \
    void test_name##_custom_wrapper();                                                                          \
                                                                                                                \
    BOOST_AUTO_TEST_CASE( test_name )                                                                           \
    {                                                                                                           \
        __try                                                                                                   \
        {                                                                                                       \
            test_name##_custom_wrapper();                                                                       \
        }                                                                                                       \
        __except(pHandler->WriteMinidumpForException(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) {}  \
    }                                                                                                           \
                                                                                                                \
    void test_name##_custom_wrapper()                                                                           \
    

    Where pHandler is a Breakpad ExceptionHandler pointer.

    The downside is you have to replace every occurrence of BOOST_AUTO_TEST_CAST with the wrapper macro. But it does the trick.