C++ Boost::Test::BOOST_AUTO_TEST_CASE














































C++ Boost::Test::BOOST_AUTO_TEST_CASE



Description : 

  Boost test is a unit test framework, It is more neat approach to test individual pieces of codes. The Boost.Test library provides both an easy to use and flexible set of interfaces for writing test programs, organizing tests into simple test cases and test suites, and controlling their runtime execution.

BOOST_AUTO_TEST_CASE  is used to declare non-parameterized test cases.

This macro automatically creates and registers the test case with the given test case name.

Manual creation and registration of test cases can also be done if needed.



Header : 

  #include <boost/test/included/unit_test.hpp>



CODE :

#define BOOST_TEST_MODULE automatic test case linking // Defines the name of the program which is used in the messages
#include <boost/test/included/unit_test.hpp>

int sum(int firstNumber, int secondNumber)
{
    return firstNumber + secondNumber;
}

BOOST_AUTO_TEST_CASE(sum_check)// Declares a test case with name sum_check which will be automatically created and registered
{
    int sumCheck = 10;
    int firstNumber[] = {1, 2, 3, 4, 5};
    int secondNumber[] = {5, 6, 7, 8, 9};

    for(int i=0; i< sizeof(firstNumber)/sizeof(int) ; i++)
    {
        BOOST_TEST(sum(firstNumber[i], secondNumber[i]) == sumCheck ); // Checks whether the value returned from method sum is equal to 10 
    }

}

//  g++ autoTestCase.cpp   - to compile
// ./a.out -l all          - to execute
// ./a.out --help          - to view other available execution options


Compile and execute :


g++ autoTestCase.cpp   - to compile
./a.out -l all         - to execute


OUTPUT :

Running 1 test case...
Entering test module "automatic test case linking"
autoTestCase.cpp(9): Entering test case "sum_check"
autoTestCase.cpp(17): error: in "sum_check": check sum(firstNumber[i], secondNumber[i]) == sumCheck has failed [6 != 10]
autoTestCase.cpp(17): error: in "sum_check": check sum(firstNumber[i], secondNumber[i]) == sumCheck has failed [8 != 10]
autoTestCase.cpp(17): info: check sum(firstNumber[i], secondNumber[i]) == sumCheck has passed
autoTestCase.cpp(17): error: in "sum_check": check sum(firstNumber[i], secondNumber[i]) == sumCheck has failed [12 != 10]
autoTestCase.cpp(17): error: in "sum_check": check sum(firstNumber[i], secondNumber[i]) == sumCheck has failed [14 != 10]
autoTestCase.cpp(9): Leaving test case "sum_check"; testing time: 200us
Leaving test module "automatic test case linking"; testing time: 248us

*** 4 failures are detected in the test module "automatic test case linking"





Comments