Frage

So, I have started using boost unit testing. When I try and build a simple test that creates an instance of a class I get a compilation error.It works fine without an instance of the class.

The compilation error message is:

/src/test/WTFomgFail_test.cpp: In member function ‘void WTFomgFail::defaultConstructor::test_method()’:
/src/test/WTFomgFail_test.cpp:20: error: expected primary-expression before ‘obj’
/src/test/WTFomgFail_test.cpp:20: error: expected `;' before ‘obj’

WTFomgFail_test.cpp:

#include "WTFomgFail.hpp"

#define BOOST_TEST_MODULE WTFomgFail
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(WTFomgFail)

BOOST_AUTO_TEST_CASE( defaultConstructor )
{
    WTFomgFail obj = WTFomgFail();
    BOOST_MESSAGE("wow, you did it");
}

BOOST_AUTO_TEST_SUITE_END()

WTFomgFail.hpp:

#ifndef WTFOMGFAIL_HPP_
#define WTFOMGFAIL_HPP_

class WTFomgFail
{
public:
    WTFomgFail();
    ~WTFomgFail();
};

#endif /* WTFOMGFAIL_HPP_ */

WTFomgFail.cpp:

#include "WTFomgFail.hpp"

WTFomgFail::WTFomgFail()
{
}

WTFomgFail::~WTFomgFail()
{
}

The error goes away if I change BOOST_AUTO_TEST_SUITE(WTFomgFail) to something else, say BOOST_AUTO_TEST_SUITE(OMGreally).

Furthermore, I dont get the error when using #define BOOST_TEST_MODULE OMGreally with BOOST_AUTO_TEST_SUITE(OMGreally).

So, my question is when using the boost UTF is naming the module, test_suite, and class the same thing explicitly forbidden?

War es hilfreich?

Lösung

I know I'm late to this question, but I just stumbled across it and it looked so lonely...

To understand this restriction, you have to understand how Boost Tests originally worked. (It still can work this way, but back then there were no BOOST_AUTO_... macros and you had to do it this way.)

From the docs:

class test_class {
public:
    void test_method1()
    {
        BOOST_CHECK( true /* test assertion */ );
    }
    void test_method2()
    {
        BOOST_CHECK( false /* test assertion */ );
    }
};

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    boost::shared_ptr<test_class> tester( new test_class );

    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &test_class::test_method1, tester )));
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &test_class::test_method2, tester )));
    return 0;
}

This was a bit cumbersome, because every time you add a test function, you had to change code in two seperate locations (the definition of the function and the registering with the test suite). The registering is also somewhat non-intuitive.

That is why they introduced the BOOST_AUTO_TEST_SUITE and BOOST_AUTO_TEST_CASE, which are doing this for you.

The parameter you pass to BOOST_AUTO_TEST_SUITE is, of course, the name of the class (test_class above). The parameter to BOOST_AUTO_TEST_CASE is the name of the test function (test_method1() and test_method2() above).

So no, those may (of course) not be identical to the class and functions you are testing. You can use namespaces for this, but personally I prefer simply suffixing the class name with Tu (or _tu if you aren't into CamelCase naming), and using that for the test suite.

I hope this helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top