Home > The Unit Test Framework > User's guide > Test organization > Test suite > Manual registration
PrevNext

Manually registered test suites

To create a test suite manually, employ the macro BOOST_TEST_SUITE:
要手動創建測試套件,使用宏 BOOST_TEST_SUITE:

BOOST_TEST_SUITE(test_suite_name)

BOOST_TEST_SUITE creates an instance of the class boost::unit_test::test_suite and returns a pointer to the constructed instance. Alternatively you can create an instance of class boost::unit_test::test_suite yourself.
BOOST_TEST_SUITE 創建類 boost::unit_test::test_suite 的一個實例,並返回指向它的指針。 你也可以選擇自己創建 boost::unit_test::test-suite 類的實例。

[Note] Note

boost::unit_test::test_suite instances have to be allocated on the heap and the compiler won't allow you to create one on the stack.
boost::unit_test::test_suite 實例需要聲明在堆上,編譯器不允許你在棧上創建。

Newly created test suite has to be registered in a parent one using add interface. Both test suite creation and registration is performed in the test module initialization function.
新創建的測試套件需要用添加接口註冊到父測試套件中。測試套件的創建和註冊都是在測試模塊初始化函數中完成的。

Example 15. Manually registered test suites

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

//____________________________________________________________________________//

void test_case1() { /* : */ }
void test_case2() { /* : */ }
void test_case3() { /* : */ }
void test_case4() { /* : */ }

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
    test_suite* ts1 = BOOST_TEST_SUITE( "test_suite1" );
    ts1->add( BOOST_TEST_CASE( &test_case1 ) );
    ts1->add( BOOST_TEST_CASE( &test_case2 ) );

    test_suite* ts2 = BOOST_TEST_SUITE( "test_suite2" );
    ts2->add( BOOST_TEST_CASE( &test_case3 ) );
    ts2->add( BOOST_TEST_CASE( &test_case4 ) );

    framework::master_test_suite().add( ts1 );
    framework::master_test_suite().add( ts2 );

    return 0;
}

//____________________________________________________________________________//
Source code | Show output
> example --log_level=test_suite
Running 4 test cases...
Entering test suite "Master Test Suite"
Entering test suite "test_suite1"
Entering test case "test_case1"
Leaving test case "test_case1"
Entering test case "test_case2"
Leaving test case "test_case2"
Leaving test suite "test_suite1"
Entering test suite "test_suite2"
Entering test case "test_case3"
Leaving test case "test_case3"
Entering test case "test_case4"
Leaving test case "test_case4"
Leaving test suite "test_suite2"
Leaving test suite "Master Test Suite"

*** No errors detected

This example creates a test tree, which can be represented by the following hierarchy:
上面的例子創建了如下層次的測試樹:


PrevUpHomeNext