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.
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>
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()
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:
TestRunner.cpp
)
TestRunner.cpp
)
controller_class_test.cpp
)
controller_class_test.cpp
)TestRunner.cpp
)
model_a_test.cpp
)
model_a_test.cpp
)model_b_test.cpp
)
model_B_test.cpp
)TestRunner.cpp
)
simple_view_test.cpp
)
simple_view_test.cpp
)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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With