Introduction
The Boost Test Library may be not the most popular but useful tool for writing unit tests. The following tip shows how to add some progress indicator to the test run.
Using the code
A simple unit test code based on the Boost Test Library looks like:
#include <boost/test/unit_test.hpp>
#define BOOST_TEST_MODULE test_module
BOOST_FIXTURE_TEST_CASE(test_1)
{
value_1=...;
BOOST_CHECK_EQUAL(value_1, 10);
}
BOOST_FIXTURE_TEST_CASE(test_2)
{
value_2=...;
BOOST_CHECK_EQUAL(value_2, 10);
}
BOOST_FIXTURE_TEST_CASE(test_100)
{
...
}
The code will execute all listed sub-tests and will print out error message if a check point failed. But if no error occurred, the code will print nothing till the very end. In the end it will print something like that:
Running 100 test cases...
*** No error detected.
To makes the framework to print progress information we can use the --show_progress=true command line option or to set the BOOST_TEST_SHOW_PROGRESS environment variable to true.
But if we want to force the progress indicator and/or customize it (for example to print dots instead of asterics or to show the progress in absolute values and not in percents), we can do it by tiny modification:
The macro BOOST_FIXTURE_TEST_CASE
accepts second parameter that is name of a class. An instance of the class will be created each time the sub-test starts and will be destroyed after the sub-test is complied. To show the progress we just need a class that prints a dot in its destructor:
#include <boost/test/unit_test.hpp>
#include <iostream>
#define BOOST_TEST_MODULE test_module
struct UF
{
UF(){}
~UF()
{
std::cout<<"."<<std::flush;
}
};
BOOST_FIXTURE_TEST_CASE(test_1, UF)
{
value_1=...;
BOOST_CHECK_EQUAL(value_1, 10);
}
BOOST_FIXTURE_TEST_CASE(test_2, UF)
{
value_2=...;
BOOST_CHECK_EQUAL(value_2, 10);
}
BOOST_FIXTURE_TEST_CASE(test_100, UF)
{
...
}
The output will be:
Running 100 test cases...
..............................................................................................
*** No error detected.