4

I have a Visual Studio project where I want to do some unit tests with Boost.Test.

And I have 2 files:

File 1:

#define BOOST_TEST_MODULE FileX

#include <boost/test/unit_test.hpp>
#include <stdio.h>

BOOST_AUTO_TEST_SUITE(test_suite_name)

BOOST_AUTO_TEST_CASE(TestFileX)
{
    BOOST_CHECK(true);
}


BOOST_AUTO_TEST_SUITE_END()

And File 2:

#define BOOST_TEST_MODULE XContainer

#include <boost/test/unit_test.hpp>
#include <stdio.h>

BOOST_AUTO_TEST_SUITE(test_suite_name2)

BOOST_AUTO_TEST_CASE(TestXContainer)
{
    BOOST_CHECK(true);
}

BOOST_AUTO_TEST_SUITE_END()

When I compile the project I get a link error that's saying that main is already defined. I noticed that main file is defined in unit_test.hpp but I need to include it for the test macros.

How should I add 2 test cases in 2 separate file?

Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
  • possible duplicate of [Boost.Test: Looking for a working non-Trivial Test Suite Example / Tutorial](http://stackoverflow.com/questions/2906095/boost-test-looking-for-a-working-non-trivial-test-suite-example-tutorial) – Arne Mertz Mar 11 '14 at 14:42

3 Answers3

10

The real problem is that BOOST_TEST_MODULE is only ever intended to be defined once in your entire test executable. Defining BOOST_TEST_MODULE also defines BOOST_TEST_MAIN which pulls in an implementation of main.

So in one single place define BOOST_TEST_MODULE to be the name of your global suite and therefore also define BOOST_TEST_MAIN to get a single implementation of main.

This is a subtlety that I will need to note in my documentation rewrite.

legalize
  • 2,214
  • 18
  • 25
  • 1
    Thanks for your documentation. Everything I needed was in "Header only" section: http://user.xmission.com/~legalize/boost.test/libs/test/doc/html/test/guide/compilation/header_only.html/index.html – stoycho Sep 12 '17 at 10:22
  • 2
    They've since rewritten the documentation for boost.test and it is much better now. At some point I will delete the content at the above link, but I'm lazy :) – legalize Sep 14 '17 at 20:49
  • I too found @legalize's documentation to be the only thing that actually explained concisely, with examples, what to do in the multi-file case. – rgov Feb 19 '19 at 02:32
1

You must use #define BOOST_TEST_DYN_LINK in every source file with tests.

zoska
  • 1,684
  • 11
  • 23
0

testXXX.h shouldn't be included in testXXX.cpp.

All of testXXX.h files should be included in main.cpp which should contain #define BOOST_TEST_MODULE TestXXXXXXXXX

Zhang
  • 3,030
  • 2
  • 14
  • 31