1

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
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • What about simply `#define BOOST_TEST_NO_MAIN`. Is it defined before your includes? Does [this](https://www.boost.org/doc/libs/1_63_0/libs/test/doc/html/boost_test/utf_reference/link_references/link_boost_test_no_main.html) help? – Tas Dec 28 '18 at 12:32

1 Answers1

2

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.

erenon
  • 18,838
  • 2
  • 61
  • 93
  • That may work but I prefer not to modify my `CMakeLists.txt` file or anything else to run my tests. I noticed that using e.g. another framework like `Catch` works only if I comment out my original `main` method to avoid a duplicate definition of `main`. I come from a `Java` background where running different test source files with an entry point each are allowed to co-exist which is quite convenient. – BullyWiiPlaza Dec 28 '18 at 14:29
  • 1
    @BullyWiiPlaza: well, that's not how it works here. Make your main function really tiny, e.g: a simple invocation of your apps "main", then you don't have this problem. – erenon Dec 28 '18 at 15:31