Running Boost.Test without main method invocation

384 views Asked by At

I'm trying to test parts of my code. I wrote the following test.h file:

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(my_test) {
    BOOST_CHECK(true);
}

If I run the test, my application's main method is invoked and since the command line arguments are missing, it terminates. I want to just run the test suite as it is and succeed since BOOST_CHECK on true should be a passed test. Once this works, I would add calls to functions from my code base one by one for regression testing. Is this possible to do? If yes, how?

This post suggests adding the following define to the top of the test.h file but it does not work for skipping the main method invocation:

#define BOOST_TEST_NO_MAIN true
2

There are 2 answers

2
erenon On BEST ANSWER

BOOST_TEST_NO_MAIN makes Boost.Test omit its own main function, therefore it will fall back to the applications main function.

In you unit tests, do not link the applications main function (do not add the file which contains the main), and let Boost.Test add its own main, which will run all your tests.

0
jaroa On

In your test class set

#define MAIN_METHOD_TEST
 ...
 ...
 <YOUR TEST CASES>
 ...
 ...
#undef MAIN_METHOD_TEST

In your tested class:

#ifdef MAIN_METHOD_TEST
int main_test(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
...
...
<YOUR MAIN METHOD CODE>
...
...
}

main method can be tested calling main_test with command line arguments as parameters.