Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

A Progress Indicator for the Boost Test Library

5.00/5 (3 votes)
20 Mar 2013CPOL1 min read 15.2K  
How to show progress in a Boost test application.

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:

C++
#include <boost/test/unit_test.hpp>   
 
#define BOOST_TEST_MODULE test_module

BOOST_FIXTURE_TEST_CASE(test_1)
{ 
 
   // calculate value_1 

   value_1=...;

   BOOST_CHECK_EQUAL(value_1, 10);
} 

BOOST_FIXTURE_TEST_CASE(test_2) 
{ 
   // calculate value_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:

C++
#include <boost/test/unit_test.hpp>   
#include <iostream>
 
#define BOOST_TEST_MODULE test_module

struct UF
{
    UF(){}
    ~UF()
    { 
        // indicate that the test 
        // was completed
        std::cout<<"."<<std::flush; 
    }
};

BOOST_FIXTURE_TEST_CASE(test_1, UF)
{ 
 
   // calculate value_1 

   value_1=...;

   BOOST_CHECK_EQUAL(value_1, 10);
} 

BOOST_FIXTURE_TEST_CASE(test_2, UF) 
{ 
   // calculate value_2 

  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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)