user1167219
user1167219

Reputation:

HOWTO start unit testing with boost C++

I have the following basic test in file:

//test.cpp
#define BOOST_TEST_MODULE test_module_name
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(first_test)
{
    int i = 1;
    BOOST_TEST(i); 
    BOOST_TEST(i == 2); 
}

Compiling and linking using gcc

gcc -L usr/local/lib/ -I usr/local/include/ \
    -c test/test.cpp -lboost_unit_test_framework \
    -lboost_test_exec_monitor -o build/test.out

When I run test.out I get the following error

bash: ./test.out: cannot execute binary file: Exec format error

I believe this issue may have something to do with the fact that this file has no main function, this is because I am expecting test exec monitor to run this test and this is not happening.

Q1) Can I get a detailed HOWTO do a basic unit test with boost

or

Q2) What is required for exec monitor to run these tests?

Upvotes: 1

Views: 606

Answers (2)

kenba
kenba

Reputation: 4539

Your test needs to be run in a test suite, e.g. BOOST_AUTO_TEST_SUITE.
I usually put one in each unit test file e.g.:

#include "via/geometry/wgs84/derived_parameters.hpp"
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(Test_WGS84)

BOOST_AUTO_TEST_CASE(test_wgs84_parameters)
{
  // primary_parameters
  BOOST_CHECK_EQUAL(6378137.0,        wgs84::a);
  BOOST_CHECK_CLOSE(0.00335281068118, wgs84::f, 0.000001); // 0.000001% tolerance

  // derived_parameters
  BOOST_CHECK_CLOSE(6356752.3141,     wgs84::b,   0.0000001);
  BOOST_CHECK_CLOSE(0.00669437999014, wgs84::e_2, 0.0000001);
  BOOST_CHECK_CLOSE(0.08181919084262, wgs84::e,   0.0000001);
}

BOOST_AUTO_TEST_SUITE_END()

You can put the BOOST_TEST_MODULE in the unit test file for the test exec monitor as you've done.
However, I prefer to put it in a separate file so that new unit tests can be added by simply adding the new test file to the build, e.g. test_main.cpp:

#ifndef _MSC_VER
#define BOOST_TEST_DYN_LINK
#endif
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE Main
#include <boost/test/unit_test.hpp>

Upvotes: 1

Kurt Stutsman
Kurt Stutsman

Reputation: 4034

You're giving the -c option which only generates the the object file. You need to link it to make an executable.

Upvotes: 1

Related Questions