views:

87

answers:

1

Hi

Does anybody know how to register my custom exception translator when using auto test cases in Boost.Test? I've found some examples (very few actually), but they do not show how to use this feature with auto test cases which are the biggest advantage of boost.test in my opinion. My example test suite:

    #define BOOST_TEST_MODULE StateMachineTest
    #define BOOST_TEST_DYN_LINK

    #include <boost/test/unit_test.hpp>

    BOOST_AUTO_TEST_SUITE (FirstTest);

    BOOST_AUTO_TEST_CASE (testBasic)
    {
            BOOST_CHECK (true);
    }

    BOOST_AUTO_TEST_SUITE_END ();

Thanks in advance.

A: 

(Note: I'm still using Boost 1.34.1)

Regardless of the AUTO_TEST_CASE feature, to register custom exception handlers you need to implement the init_unit_test_suite main function. (You do not need to register any of your auto tests there.)

All my unit tests projects use a ut_main.cpp file that contains (roughly) the following: (This is in addition to all the other cpp files containing the actual auto tests.)

void translate_mfc_exception(CException* pMfcEx) {
  ...
  BOOST_ERROR(msg);
}
// ...
using namespace ::boost::unit_test;
test_suite* init_unit_test_suite(int argc, char* argv[])
{

  // Initialize global Handlers:
  unit_test_monitor.
    register_exception_translator<CException*>( &translate_mfc_exception );

  // Return dummy suite to make framework happy:
  test_suite* test = BOOST_TEST_SUITE( "Empty Test Suite" );
  return test;
}

This should be all you need in addition to your auto test cases.

Martin