Boost test case and suite fixtures in manually def

2019-05-28 13:08发布

问题:

Using Boost 1.46.1 on Windows x86, Android TI 2.2

I have defined my own test suite tree, since I need the user to choose order of the tests. although I'm aware the tests should be independent, this is a requirement. The test suite tree was redefined using my own implementation of test_suite* init_unit_test_suite(int, char**).

For automated test cases and automated test suites, there are Boost macros: BOOST_FIXTURE_TEST_CASE and BOOST_FIXTURE_TEST_SUITE( suite_name, F ). These macros register the function to the framework::master_test_suite(), which is undesired behavior in this case.

Global fixture (BOOST_GLOBAL_FIXTURE(fixure_name)) remains unaffected in manual test suite definition.

I would like to use fixtures in Boost Unit Testing Framework for manually defined test suites and cases. A neat way.

There are some workarounds:

  • Test Suite Fixture - can be defined as a first and last test among it's children suites/cases. This however affects the test results and acts as a separate test, which is not really a fine solution.
  • Test Case Fixture - by wrapping a scoped instance around the test case function.

Is there any other, cleaner and nicer solution to my problem? I don't really have resources to dig deep into Boost library. On the other hand, I don't want to significantly decrease the quality and readability of the code on my side.

Regards, LK

#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/test/unit_test.hpp>
using namespace boost::unit_test;

BOOST_GLOBAL_FIXTURE(GFixture);

test_suite* init_unit_test_suite( int, char** )
{
    test_suite* ts1 = BOOST_TEST_SUITE( "Suite1" );

    boost::shared_ptr<TestClass1> test1 ( new TestClass1 );
    ts1->add( BOOST_TEST_CASE( boost::bind(&TestClass1::Run, test1)));
    boost::shared_ptr<TestClass2> test2 ( new TestClass2 );
    ts1->add( BOOST_TEST_CASE( boost::bind(&TestClass2::Run, test2)));
    boost::shared_ptr<TestClass3> test3 ( new TestClass3);
    ts1->add( BOOST_TEST_CASE( boost::bind(&TestClass3::Run, test3)));

    framework::master_test_suite().add( ts1 );
    return 0;
}

Unit Test Framework: User's guide
http://www.boost.org/doc/libs/1_46_1/libs/test/doc/html/utf/user-guide.html

回答1:

consider the following code:

#define BOOST_TEST_MAIN
#include <boost/test/included/unit_test.hpp>
#include <boost/test/unit_test_suite.hpp>

BOOST_GLOBAL_FIXTURE( ... );

BOOST_AUTO_TEST_SUITE( Suite1 )

    BOOST_AUTO_TEST_CASE( Test1 )
    {
        TestClass1 testClass;

        testClass.Run();
    }


    BOOST_AUTO_TEST_CASE( Test2 )
    {
        TestClass2 testClass;

        testClass.Run();
    }

    BOOST_AUTO_TEST_CASE( Test3 )
    {
        TestClassT testClass;

        testClass.Run();
    }

BOOST_AUTO_TEST_SUITE_END()

This is enough to perform test.