Search code examples
boostboost-test

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


In Boost.Test, how can I obtain the name of the current auto test case?

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 the example, I want the variable test_name to contain "MyTest".


Solution

  • There is an undocumented* function that may be called for that purpose. The following line will flush 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 flush the parameters in case of parametrized tests.

    You might also be interested in the test checkpoints** (which seems to be 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 current_test_case() function is now documented, see the official Boost documentation.

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