Search code examples
c++unit-testingboostboost-test

How to define Boosts tests to run in code?


Using Boost.Test I'm trying to specify the BOOST_TESTS_TO_RUN environment variable in code, so that only some tests will be executed. Using #define BOOST_TESTS_TO_RUN.

The command line parameter --run_tests works fine. But I'd like to do it via the environment variable in order to have a single main.cpp in which I can select different suites and modules.

But I'm not sure on how to specify it, or where. Should it be before including boost? What if I have different cpp files with different test suites?

module_A.cpp

#include "SomeHeader.h"
#define BOOST_TESTS_TO_RUN // ??
#include "boost/test/unit_test.hpp"
BOOST_AUTO_TEST_SUITE( moduleA );
BOOST_AUTO_TEST_CASE( test1 ){}
BOOST_AUTO_TEST_CASE( test2 ){}
BOOST_AUTO_TEST_CASE( test3 ){}
BOOST_AUTO_TEST_SUITE_END();

Solution

  • The Boost UTF was not designed that way. If you really want to en-/disable certain tests from your code, you could use manual registration for all your tests and define a master test suite, in which you need to manually register all tests. See the master-test-suite documentation and other pages about manual test registration. You could then comment-out certain tests you don't want to run.

    However, I do not recommend this approach. It's too easy to forget tests and thus harder to maintain. For debugging you could easily run the test executable by hand and use the command-line parameters.