VansFannel
VansFannel

Reputation: 45921

Boost Test usage variants

I'm using Visual Studio 2017 Enterprise 15.9.12 and Visual C++ 2017 00369-90013-89248-AA631 and Boost version 1.71.0.

I have a unit test project, and everything goes find when I had only one file (AstroTimeTest.cpp). But, when I have added another file (CoordinateSystemsTest.cpp) I get some LINKER errors. To create the second file I have copied some code from the first file.

AstroTimeTest.cpp contents:

#define BOOST_TEST_MODULE astroTimeTests
#include <boost/test/included/unit_test.hpp>
#include <iostream>

#include "../../AstroTime/AstroTime.h"

struct TestFixture
{
    AstroTime time_instance;

    TestFixture()
        : time_instance()
    {}

    ~TestFixture() = default;
};

bool operator ==(astTime const &left, astTime const &right)
{
    return(
        left.secs == right.secs
        && left.min == right.min
        && left.hour == right.hour);
}

std::ostream& operator<<(std::ostream& os, const astTime& dt)
{
    os << dt.hour << "h " << dt.min << "m " << dt.secs << "s" << std::endl;

    return os;
}

bool operator ==(dateTime const &left, dateTime const &right)
{
    return(
        left.t.secs == right.t.secs
        && left.t.min == right.t.min
        && left.t.hour == right.t.hour
        && left.day == right.day
        && left.mon == right.mon
        && left.year == right.year
        && left.daylightSaving == right.daylightSaving
        && left.timeZone == right.timeZone);
}

std::ostream& operator<<(std::ostream& os, const dateTime& dt)
{
    os << dt.t.hour << " " << dt.t.min << " " << dt.t.secs << ", "
        << dt.day << " " << dt.mon << " " << dt.year << std::endl;

    return os;
}

BOOST_FIXTURE_TEST_SUITE(TestAstroTime, TestFixture)

BOOST_AUTO_TEST_CASE(ToDecimalHour_1)
{
    double decHour = 18.524167;
    astTime t = {18, 31, 27.0 };

    BOOST_REQUIRE_EQUAL(decHour, time_instance.ToDecimalHour(t));
}

// *** Removed for brevety **/

BOOST_AUTO_TEST_SUITE_END()

CoordinateSystemsTest.cpp contents:

#define BOOST_TEST_MODULE coordinateSystemsTests
#include <boost/test/included/unit_test.hpp>
#include <iostream>

#include "../../AstroTime/CoordinateSystems.h"


struct TestFixture
{
    CoordinateSystems coord_instance;

    TestFixture()
        : coord_instance()
    {}

    ~TestFixture() = default;
};

bool operator ==(angle const &left, angle const &right)
{
    return(
        left.secs == right.secs
        && left.min == right.min
        && left.degree == right.degree);
}

std::ostream& operator<<(std::ostream& os, const angle& dt)
{
    os << dt.degree << "º " << dt.min << "' " << dt.secs << "\"" << std::endl;

    return os;
}


BOOST_FIXTURE_TEST_SUITE(TestCoordinateSystems, TestFixture)



BOOST_AUTO_TEST_CASE(ToDecimalDegrees_1)
{
    double decDegrees = 182.524167;
    angle degrees = { 182, 31, 27.0 };

    BOOST_REQUIRE_EQUAL(decDegrees, coord_instance.ToDecimalDegrees(degrees));
}




BOOST_AUTO_TEST_SUITE_END()

One of the errors I get is:

CoordinateSystemsTest.obj : error LNK2005: "public: __thiscall boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(class boost::unit_test::test_unit_generator const &,class boost::unit_test::decorator::collector_t &)" (??0auto_test_unit_registrar@ut_detail@unit_test@boost@@QAE@ABVtest_unit_generator@23@AAVcollector_t@decorator@23@@Z) already defined in AstroTimeTest.obj

If I remove all the code in CoordinateSystemsTest.cpp, I still get the same LINKER error.

But If I remove also this line:

#include <boost/test/included/unit_test.hpp>

Leaving the file almost empty, I don't get any error.

The error is due to Boost Test Usage Variants, because I'm using the "Single-header usage variant". But I don't understand what variant do I have to use.

How can I use more than one CPP file with Boost Test?

Upvotes: 2

Views: 762

Answers (2)

ecatmur
ecatmur

Reputation: 157344

Per Header-only with multiple translation units:

It is possible to use the header-only variant of the Unit Test Framework even if the test module has multiple translation units:

  • one translation unit should define BOOST_TEST_MODULE and include <boost/test/included/unit_test.hpp>
  • all the other translation units should include <boost/test/unit_test.hpp>

I would recommend having a dedicated TU solely containing

#define BOOST_TEST_MODULE header-only multiunit test
#include <boost/test/included/unit_test.hpp>

(plus global fixtures, etc.) and all your actual tests in other TUs.

Upvotes: 2

Raviteja Narra
Raviteja Narra

Reputation: 456

Could you try including the files in the respective .h files? Please also make sure that your header files have #pragma once to remove multiple inclusions.

From my experience adding headers with class definitions in c++ files cause this linker issue

Upvotes: 0

Related Questions