Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++
Tip/Trick

A Progress Indicator for the Boost Test Library

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
20 Mar 2013CPOL1 min read 14.8K   3   2
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)


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Question--show_progress Pin
Lukas Wöhrl19-Mar-13 14:42
Lukas Wöhrl19-Mar-13 14:42 
AnswerRe: [My vote of 1] --show_progress Pin
cocaf20-Mar-13 8:42
professionalcocaf20-Mar-13 8:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.