C++ Boost::Test














































C++ Boost::Test



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.


Header : 

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


 Usage Variants : 

   Boost Test provides three different usage variants for Test library. The choice of variant can be made as per individual requirements


  • Header-only variant
  • Static library variant
  • Shared library variant


Header-only variant : 

   This variant can be used if you prefer to avoid the compilation of standalone library. The header can be directly used as below.


   #define BOOST_TEST_MODULE test module name

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


BOOST_TEST_MODULE macro needs to be defined before the include. It indicates the name of the test module. This name can include spaces and quotes are not necessary 


 

Static Library usage variant : 

    Users who have access to pre-built static library can use this variant by using the following two lines and linking the static library while compiling.


    #define BOOST_TEST_MODULE test module name

    #include <boost/test/unit_test.hpp>


 Compilation : 

 g++ staticLibraryVariant.cpp /usr/local/lib/libboost_unit_test_framework.a 

 The library path may vary.


 

Shared Library usage variant :

    When there are a large number of test modules the static library variant may cause waste of disk space, shared library variant can address that issue. 

 

    #define BOOST_TEST_MODULE test module name

    #define BOOST_TEST_DYN_LINK

    #include <boost/test/unit_test.hpp>


 Compilation : 

 g++ staticLibraryVariant.cpp /usr/local/lib/libboost_unit_test_framework.so

 The library path may vary.



Note: The below three code snippets are three different files.


https://www.boost.org/doc/libs/1_69_0/libs/test/doc/html/boost_test/adv_scenarios/build_utf.html

The above link explains how to build the boost::test library as a separately compiled one. This must be followed to use the static and shared library usage variants.


CODE :


/************************* HEADER-ONLY *************************/

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

int number()
{
return 10;
}

BOOST_AUTO_TEST_CASE(first_test) // Declares a test case with name "first_test"
{
int i = 1;
BOOST_TEST(i); // Checks if i is non-zero

BOOST_TEST(number() == 2); // Checks whether the value returned from number method is 2
}

// g++ headerOnly.cpp - to compile

OUTPUT :
Running 1 test case...
headerOnly.cpp(16): error: in "first_test": check number() == 2 has failed [10 != 2]

*** 1 failure is detected in the test module "header only test"

/***************************************************************/




/************************* STATIC LIBRARY *************************/
#define BOOST_TEST_MODULE static library test // Defines the name of the program which is used in the messages
#include <boost/test/unit_test.hpp>
int number()
{
return 10;
}

BOOST_AUTO_TEST_CASE(static_test) // Declares a test case with name "static_test"
{
BOOST_TEST(number() == 10); // Checks whether the value returned from number method is 10
}

// To compile use below command
// g++ staticLibraryVariant.cpp /usr/local/lib/libboost_unit_test_framework.a

OUTPUT :
Running 1 test case...

*** No errors detected
/***************************************************************/




/************************* SHARED LIBRARY *************************/
#define BOOST_TEST_MODULE shared library test // Defines the name of the program which is used in the messages
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
int number()
{
return 10;
}

BOOST_AUTO_TEST_CASE(test)
{
BOOST_TEST(number() == 2); // Checks whether the value returned from number method is 2
BOOST_TEST(number() == 10); // Checks whether the value returned from number method is 10
}

// To compile use below command
// g++ sharedLibraryVariant.cpp /usr/local/lib/libboost_unit_test_framework.so

OUTPUT :
Running 1 test case...
sharedLibraryVariant.cpp(12): error: in "first_test": check number() == 2 has failed [10 != 2]

*** 1 failure is detected in the test module "shared library test"


/***************************************************************/





Comments