The Boost.Test documentation and examples don't really seem to contain any non-trivial examples and so far the two tutorials I've found here and here while helpful are both fairly basic.
I would like to have a master test suite for the entire project, while maintaining per module suites of unit tests and fixtures that can be run independently. I'll also be using a mock server to test various networking edge cases.
I'm on Ubuntu 8.04, but I'll take any example Linux or Windows since I'm writing my own makefiles anyways.
Edit
As a test I did the following:
// test1.cpp #define BOOST_TEST_MODULE Regression #include <boost/test/included/unit_test.hpp> BOOST_AUTO_TEST_SUITE(test1_suite) BOOST_AUTO_TEST_CASE(Test1) { BOOST_CHECK(2 < 1); } BOOST_AUTO_TEST_SUITE_END() // test2.cpp #include <boost/test/included/unit_test.hpp> BOOST_AUTO_TEST_SUITE(test2_suite) BOOST_AUTO_TEST_CASE(Test1) { BOOST_CHECK(1<2); } BOOST_AUTO_TEST_SUITE_END()
Then I compile it: g++ test1.cpp test2.cpp -o tests
This gives me about a bazillion "multiple definition of" errors during linking.
When it's all in a single file it works fine.
Create a Boost. cpp file for your tests, right-click on the project node in Solution Explorer and choose Add > New Item. In the Add New Item dialog, expand Installed > Visual C++ > Test. Select Boost. Test, then choose Add to add Test.
You can simply add the cpp file to your project, and as long as it has TEST_CASE , it is executed. Show activity on this post. You could start with no other library at all, just write a program that calls your function with different values and check the result if it matches the expectation.
C++ Unit Testing With Boost.Test
(permanent link: http://web.archive.org/web/20160524135412/http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/)
The above is a brilliant article and better than the actual Boost documentation.
Edit:
I also wrote a Perl script which will auto-generate the makefile and project skeleton from a list of class names, including both the "all-in-one" test suite and a stand alone test suite for each class. It's called makeSimple and can be downloaded from Sourceforge.net.
What I found to be the basic problem is that if you want to split your tests into multiple files you have to link against the pre-compiled test runtime and not use the "headers only" version of Boost.Test. You have to add #define BOOST_TEST_DYN_LINK
to each file and when including the Boost headers for example use <boost/test/unit_test.hpp>
instead of <boost/test/included/unit_test.hpp>
.
So to compile as a single test:
g++ test_main.cpp test1.cpp test2.cpp -lboost_unit_test_framework -o tests
or to compile an individual test:
g++ test1.cpp -DSTAND_ALONE -lboost_unit_test_framework -o test1
.
// test_main.cpp #define BOOST_TEST_DYN_LINK #define BOOST_TEST_MODULE Main #include <boost/test/unit_test.hpp> // test1.cpp #define BOOST_TEST_DYN_LINK #ifdef STAND_ALONE # define BOOST_TEST_MODULE Main #endif #include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_SUITE(test1_suite) BOOST_AUTO_TEST_CASE(Test1) { BOOST_CHECK(2<1); } BOOST_AUTO_TEST_SUITE_END() // test2.cpp #define BOOST_TEST_DYN_LINK #ifdef STAND_ALONE # define BOOST_TEST_MODULE Main #endif #include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_SUITE(test2_suite) BOOST_AUTO_TEST_CASE(Test1) { BOOST_CHECK(1<2); } BOOST_AUTO_TEST_SUITE_END()
I don't know what else you really need than what's in the later tutorial. I've done everything I need to in just that way. Not sure I understand your description either.
One thing that you might be asking for is the ability to have more than one .cpp file in your test program. That's as simple as only defining BOOST_TEST_MODULE in one of those .cpp files. We have a "driver.cpp" file in all our test programs that just defines that and includes the unit test header. All the rest of the .cpp files (scoped by module or concept) only include the unit test header, they do not define that variable.
If you want to both be able to compile them together and compile them separately then you could use your own -D variable to define BOOST_TEST_MODULE or not.
If you're looking for a way to run a bunch of test programs in one single run and get a report then you could look at the automake method of doing tests or, better yet, the CMake method (CTest). Pretty sure you can use CTest from your own makefile if you insist.
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