Exception translator

Does anyone know how to register my custom translator when using auto-tests in Boost.Test? I found some examples (actually very few), but they do not show how to use this function with self-tests, which, in my opinion, are the biggest advantage of boost.test. My 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.

+4
source share
2 answers

(Note: I'm still using Boost 1.34.1)

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

All of my unit test projects use the ut_main.cpp file, which contains (approximately) the following: (This is in addition to all other cpp files containing the actual automatic 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; } 

That should be all you need in addition to your autotests.

+1
source

Alternatively, you can register a translator in a global device

+1
source

Source: https://habr.com/ru/post/1302784/


All Articles