Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve organisation of test suites and cases with Boost?

Preface

I'm new to Unit Testing and rather new to C++. Recently I had some experience with test driven development in Ruby using RSpec. Now I'm trying getting similar working in C++ with Boost's unit testing framework.

Scenario

I'm organising my header and source files of the application in a directory /src under the project's root folder. As I've seen it multiple times around in other C++ programs, having the tests in a directory /tests in the project's root directory seems reasonable.

Now I want to replicate the directory structure of the source files as well in the tests. Thus, assume I've got the following source/header file structure:

/src
  /controller
    controller_class.h
    controller_class.cpp
  /model
    model_a.h
    model_a.cpp
    model_b.h
    model_b.cpp
  /view
    simple_view.h
    simple_view.cpp

And thus the tests are organised as followed

/tests
  TestRunner.cpp
  /controller
    controller_class_test.cpp
  /model
    model_a_test.cpp
    model_b_test.cpp
  /view
    simple_view_test.cpp

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

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "MyProgram Unit Tests"
#include <boost/test/unit_test.hpp>

Problem

I now thought to continue in TestRunner.cpp with the creation of the basic test suits (for controller, model and view) as follows

BOOST_AUTO_TEST_SUITE ( controller )
  //some stuff here
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE ( model )
  //some stuff here
BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE ( view )
  //some stuff here
BOOST_AUTO_TEST_SUITE_END()

Desire

But how can I now integrate further nested test suites and cases into these top-level test suits? I finally want to have the actual test cases only appear in the *_test.cpp files. While each of those files wrap the test cases into one additional test suite:

  • Master Test Module (TestRunner.cpp)
    • Controller Test Suite (TestRunner.cpp)
      • Controller Class Test Suite (controller_class_test.cpp)
        • Controller Class Test Cases (controller_class_test.cpp)
    • Model Test Suite (TestRunner.cpp)
      • Model A Test Suite (model_a_test.cpp)
        • Model A Test Cases (model_a_test.cpp)
      • Model B Test Suite (model_b_test.cpp)
        • Model B Test Cases (model_B_test.cpp)
    • View Test Suite (TestRunner.cpp)
      • Simple View Test Suite (simple_view_test.cpp)
        • Simple View Test Cases (simple_view_test.cpp)

Question

How do I have to include the nested suits and cases in the respective higher level suite? I could not find anything in the Boost documentation, though The Unit Test Framework > User's guide > Test organization > Test suite > Automated registration came pretty close.
In Ruby with RSpec one just needs to place the test files (read: *_spec.rb) the way I did and its automatically iterating through them. I guess, with Boost I have to explicitly declare it that way.

like image 710
Torbjörn Avatar asked Feb 15 '12 22:02

Torbjörn


1 Answers

Boost.Test test suites are similar to C++ namespaces. You can restart them any time. Each of your test files just need to place test cases in correct test suites:

controller_class_a_test.cpp:

 BOOST_AUTO_TEST_SUITE( controller )

 BOOST_AUTO_TEST_CASE( test_class_a )
 {
 }

 BOOST_AUTO_TEST_SUITE_END()


controller_class_b_test.cpp:

 BOOST_AUTO_TEST_SUITE( controller )

 BOOST_AUTO_TEST_CASE( test_class_b )
 {
 }

 BOOST_AUTO_TEST_SUITE_END()

The same concept applies to the test tree of any depth. Also you do not need top level TestRunner.cpp at all. Just combine all your test files into a single test module and Boost.Test will take care about the rest.

like image 97
Gennadiy Rozental Avatar answered Oct 04 '22 19:10

Gennadiy Rozental