Warning: file() [function.file]: Couldn't resolve host name in /home/sharpr6/public_html/wp-content/themes/starscape/code/starscape.php on line 419

Warning: file(http://upgrade.starscapetheme.com/check.php?v=1.5.6&b=http%3A%2F%2Fwww.sharprobotica.com) [function.file]: failed to open stream: operation failed in /home/sharpr6/public_html/wp-content/themes/starscape/code/starscape.php on line 419

Warning: strlen() expects parameter 1 to be string, array given in /home/sharpr6/public_html/wp-content/themes/starscape/code/starscape.php on line 450
Including Boost.Test in Your C++ Project | SharpRobotica.com - Sharp ideas for the software side of robotics

Including Boost.Test in Your C++ Project

datePosted on 05:08, April 20th, 2010 by Billy McCafferty

It’s certainly hard to find much fault with the libraries of Boost.  They’re easy to include, they add a terrific amount of power to the STL, and they come with extensive documentation and a thriving community to boost your efforts.  (Awkward pun intended.)  Just about the only library (so far) that I’ve had a hard time finding clear, concise, introductory materials on is Boost.Test.  Boost.Test is a terrific library for facilitating unit testing with your C++ project.  Once you get it setup in your project, Boost.Test makes it very easy to develop and organize unit tests for your project.  But while the Boost.Test documentation will make you a guru with the library, it’s quite a bit lacking in guiding you in getting it going in your project for the very first time.  Googling for direction will lead you (er…led me anyway) down a number of paths which seemed to be overly complex…hopefully this post will help a few people in a similar situation that I was in.

What follows are the fewest steps that I was able to determine for including Boost.Test in your C++ project.  My setup was using CMake 2.8 on Ubuntu 9.04.

  1. Download and install Boost following Boost’s online documentation.
  2. Create a folder for your tests, e.g., my_project_tests and include a file in it called TestRunner.cpp containing the code below.   When compiled, this “main” class invokes Boost to generate a true main() function for your unit tests and establishes the central testing module.  TestRunner.cpp won’t actually contain any tests, it simply consolidates initialization code that would otherwise be duplicated across your test files and better allows them to be run together.
    #define BOOST_TEST_MODULE "my_project Unit Tests"
    #include <boost/test/unit_test.hpp>
  3. Create a file called ExampleTests.cpp in the same folder containing the code below.  This is taken directly from the Boost.Test tutorial with the omission of the test module #define, which was done via TestRunner.cpp.  To add any more tests, you simply create new .cpp files to hold the respective tests and include a reference to the files in your CMakeLists.txt file, as described in the next step.  But first, ExampleTests.cpp:
    #include <boost/test/unit_test.hpp>
    int add( int i, int j ) { return i+j; }
    BOOST_AUTO_TEST_CASE( my_test )
      // seven ways to detect and report the same error:
      BOOST_CHECK( add( 2,2 ) == 4 );        // #1 continues on error
      BOOST_REQUIRE( add( 2,2 ) == 4 );      // #2 throws on error
      if( add( 2,2 ) != 4 )
         BOOST_ERROR( "Ouch..." );            // #3 continues on error
      if( add( 2,2 ) != 4 )
        BOOST_FAIL( "Ouch..." );             // #4 throws on error
      if( add( 2,2 ) != 4 ) throw "Ouch..."; // #5 throws on error
      BOOST_CHECK_MESSAGE( add( 2,2 ) == 4,  // #6 continues on error
        "add(..) result: " << add( 2,2 ) );
      BOOST_CHECK_EQUAL( add( 2,2 ), 4 );      // #7 continues on error
  4. In the folder containing TestRunner.cpp, create a CMakeLists.txt containing the code below; this assumes that the CMake version and your project name is defined in a CMakeLists.txt in a parent folder.
    # Include subdirectories for headers
    # Create the unit tests executable
     # Key includes for setting up Boost.Test
     # Just for handy reference
    # Link the libraries
     # "mt" designates that multi-threaded is supported with this Boost library
  5. Now, simply cmake, make and run the resulting executable to run your unit tests.

Happy testing!

Billy McCafferty

categoryPosted in TDD, Tools | printPrint
Related Posts:

4 Responses to “Including Boost.Test in Your C++ Project”

  1. Volker Wedemeier on September 27th, 2010 at 9:23 am

    Hi Bill,

    thanks a lot for the short tutorial which helped me over the last problem with my boost-test-cases. Now my boost tests work!

    I’d like to add one note / improvement:
    Instead of writing absolute paths in the include_direcotries statement and giving the absolute name of the test library, it would probably be better (and definately more portable) to use the lines:

    find_package( Boost REQUIRED COMPONENTS unit_test_framework)

    include_directories( ${Boost_INCLUDE_DIRS}

    target_link_libraries( my_project_tests

    in the CMakeLists.txt file. This will usually find the include path and the required library where ever it is installed in the system.

  2. Billy McCafferty on September 27th, 2010 at 10:08 pm

    Much cleaner…thanks for the tip!

  3. Tim Kelsey on May 30th, 2011 at 1:22 pm

    Just wanted to say thanks for this, very handy information.

  4. [...] For the TestRunner.cpp I took the example from this blog post: [...]

Leave a Reply

Name: (required)
Email: (required) (will not be published)

© 2011-2014 Codai, Inc. All Rights Reserved