Question

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".

Was it helpful?

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).

OTHER TIPS

A different question about suite names gives a way to extract the name rather than just printing it:

auto test_name = std::string(boost::unit_test::framework::current_test_case().p_name)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top