Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 359
Frank Avatar asked May 13 '12 19:05

Frank


People also ask

What is boost test?

Boost unit testing framework (Boost. Test) is a part of the Boost library. It is a fully-functional and scalable framework, with wide range of assertion macros, XML output, and other features. Boost. Test itself lacks mocking functionality, but it can be combined with stand-alone mocking frameworks such as gmock.


1 Answers

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

like image 177
Raffi Avatar answered Nov 16 '22 06:11

Raffi