BOOST_CHECK_NO_THROW how to get an exception message

When I test a method using

BOOST_CHECK_NO_THROW( method_to_test() ); 

and an exception is thrown, it shows that an exception was thrown, but there was never an exception message:

 test.cpp(14): error in "test": incorrect exception my_exception is caught 

Is it possible to print an exception message, i.e. the string returned by my_exception.what() ? my_exception is my_exception from std::exception and overloads what() .

+8
c ++ unit-testing boost-test
source share
2 answers

I read the boost headers a bit and redefined BOOST_CHECK_NO_THROW_IMPL in my own header file, which I use in the project to override the behavior of boost. Now it looks like this:

 #ifndef _CATCH_BOOST_NO_THROW_H_ #define _CATCH_BOOST_NO_THROW_H_ #include <boost/test/unit_test.hpp> #include <sstream> #include <string> #define BOOST_CHECK_NO_THROW_IMPL( S, TL ) \ try { \ S; \ BOOST_CHECK_IMPL( true, "no exceptions thrown by " BOOST_STRINGIZE( S ), TL, CHECK_MSG ); } \ catch( const std::exception & e ) { \ std::stringstream ss; \ ss << std::endl \ << "-----------------------------------------------" << std::endl \ << "test case: " << boost::unit_test::framework::current_test_case().p_name << std::endl \ << std::endl << "exception message: " << e.what() << std::endl; \ BOOST_TEST_MESSAGE(ss.str()); \ BOOST_CHECK_IMPL( false, "exception thrown by " BOOST_STRINGIZE( S ), TL, CHECK_MSG ); \ } \ catch( ... ) { \ std::stringstream ss; \ ss << std::endl \ << "-----------------------------------------------" << std::endl \ << "test case: " << boost::unit_test::framework::current_test_case().p_name << std::endl \ << std::endl << "exception message : <unknown exception>" << std::endl; \ BOOST_TEST_MESSAGE(ss.str()); \ BOOST_CHECK_IMPL( false, "exception thrown by " BOOST_STRINGIZE( S ), TL, CHECK_MSG ); \ } \ /**/ #define BOOST_WARN_NO_THROW( S ) BOOST_CHECK_NO_THROW_IMPL( S, WARN ) #define BOOST_CHECK_NO_THROW( S ) BOOST_CHECK_NO_THROW_IMPL( S, CHECK ) #define BOOST_REQUIRE_NO_THROW( S ) BOOST_CHECK_NO_THROW_IMPL( S, REQUIRE ) #endif // _CATCH_BOOST_NO_THROW_H_ 

Minuses: it works until there are no changes in BOOST _ * _ NO_THROW

and

an exception message will be printed before it is flagged as an error in the test output. First of all, it looks a bit pitti, so I group the output, write "---" in the upstream to improve reading. But the code after BOOST_CHECK_IMPL will never be reached.

The solution above works pretty well for me. Feel free to use if you have the same whish =)

(Using CDash to output ctest, don't forget to increase the test output limit or just disable the limit: http://web.archiveorange.com/archive/v/5y7PkVuHtkmVcf7jiWol )

+5
source share

I was alarmed by the same problem with BOOST_REQUIRE_NO_THROW . I solved this by simply deleting BOOST_REQUIRE_NO_THROW . This leads to the conclusion, for example:

 unknown location(0): fatal error in "TestName": std::runtime_error: Exception message 

and aborts the test (but comes with the following text), which is what I wanted. This does not help much if you want to use BOOST_CHECK_NO_THROW or BOOST_WARN_NO_THROW, however.

+5
source share

All Articles