In Boost.Test, how to get the name of the current test?

In Boost.Test , how can I get the name of the current autotest?

Example:

 #include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_CASE(MyTest) { std::cerr << "Starting " << test_name << std::endl; // lots of code here std::cerr << "Ending " << test_name << std::endl; } 

In this example, I want the test_name variable test_name contain "MyTest".

+8
boost boost-test
source share
2 answers

There is an undocumented * function that can be called for this purpose. The following line will clear the name of the current test to cerr :

 #include <boost/test/framework.hpp> ... std::cerr << boost::unit_test::framework::current_test_case().p_name << std::endl; 

Note, however, that using this API does not reset the parameters in the case of parameterized tests.

You may also be interested in breakpoints (which is similar to what you want to do).

 #include <boost/test/included/unit_test.hpp> ... BOOST_AUTO_TEST_CASE(MyTest) { BOOST_TEST_CHECKPOINT("Starting"); // lots of code here BOOST_TEST_CHECKPOINT("Ending"); } 

EDIT

* The function current_test_case() documented, see the official Boost documentation .

** BOOST_TEST_CHECKPOINT previously called BOOST_CHECKPOINT . See Boost changelog (1.35.0) .

+17
source share

Another question about package names provides a way to extract the name, not just print it:

 auto test_name = std::string(boost::unit_test::framework::current_test_case().p_name) 
0
source share

All Articles