Click here to Skip to main content
Click here to Skip to main content
Go to top

A Progress Indicator for the Boost Test Library

, 20 Mar 2013
Rate this:
Please Sign up or sign in to vote.
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:

#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:

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

Share

About the Author

cocaf

Canada Canada
No Biography provided

Comments and Discussions

 
Question--show_progress [modified] PinmemberLukas Wöhrl19-Mar-13 14:42 
Why don't you use the commandline option "--show_progress"?

modified 20-Mar-13 16:58pm.

AnswerRe: [My vote of 1] --show_progress Pinmembercocaf20-Mar-13 8:42 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 20 Mar 2013
Article Copyright 2013 by cocaf
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid