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

Test suites with automated registration

The solution the UTF presents for automated test suite creation and registration is designed to facilitate multiple points of definition, arbitrary test suites depth and smooth integration with automated test case creation and registration. This facility should significantly simplify a test tree construction process in comparison with manual explicit registration case.
為了實現測試套件的自動創建和註冊,UTF 設計了多點定義、測試套件任意深度以及和測試用例創建註冊的平滑集成。 和手動註冊相比,這個工具可以顯著地簡化測試樹的創建過程。

The implementation is based on the order of file scope variables definitions within a single compilation unit. The semantic of this facility is very similar to the namespace feature of C++, including support for test suite extension. To start test suite use the macro BOOST_AUTO_TEST_SUITE. To end test suite use the macro BOOST_AUTO_TEST_SUITE_END. The same test suite can be restarted multiple times inside the same test file or in a different test files. In a result all test units will be part of the same test suite in a constructed test tree.
實現基於在單個編譯單元中文件範圍內變量定義的順序。 這個工具的主義和 C++ 的命名空間特性類似,包括支持測試套件擴展。 要開始測試套件,使用宏 BOOST_AUTO_TEST_SUITE。要結束測試套件,使用宏 BOOST_AUTO_TEST_SUITE_END。 一個測試套件可以在同一個測試文件,或不同測試文件中開始多次。 其結果是所有的測試單元都是被構造的測試樹中相同的測試套件的一部分。

BOOST_AUTO_TEST_SUITE(test_suite_name)
BOOST_AUTO_TEST_SUITE_END()

Test units defined in between test suite start and end declarations become members of the test suite. A test unit always becomes the member of the closest test suite declared. Test units declared at a test file scope become members of the master test suite. There is no limit on depth of test suite inclusion.
測試套件開始和結束聲明之間定義的測試單元都萬畝測試套件的成員。 測試單元總是成為最近聲明的測試套件的成員。文件範圍內聲明的測試單元成為主測試套件的成員。 測試套件包含的深度沒有限制。

Example 16. Test suites with automated registration

This example creates a test tree that matches exactly the one created in the manual test suite registration example. As you can see test tree construction in this example is more straightforward and automated.
這個例子創建了和手動註冊測試套件示例完全相同的測試樹。 你會發現,這個例子中測試樹的構造更加直接和自動化。

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

BOOST_AUTO_TEST_SUITE( test_suite1 )

BOOST_AUTO_TEST_CASE( test_case1 )
{
    BOOST_WARN( sizeof(int) < 4 );
}

BOOST_AUTO_TEST_CASE( test_case2 )
{
    BOOST_REQUIRE_EQUAL( 1, 2 );
    BOOST_FAIL( "Should never reach this line" );
}

BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE( test_suite2 )

BOOST_AUTO_TEST_CASE( test_case3 )
{
    BOOST_CHECK( true );
}

BOOST_AUTO_TEST_CASE( test_case4 )
{
    BOOST_CHECK( false );
}

BOOST_AUTO_TEST_SUITE_END()
Source code | Show output
> example
Running 4 test cases...
test.cpp(12): fatal error in "test_case2": critical check 1 == 2 failed [1 != 2]
test.cpp(27): error in "test_case4": check false failed

*** 2 failures detected in test suite "example"

Example 17. Example of test suite extension using automated registration facility

In this example test suite test_suite consists of two parts. Their definition is remote and is separated by another test case. In fact these parts may even reside in different test files. The resulting test tree remains the same. As you can see from the output both test_case1 and test_case2 reside in the same test suite test_suite.
在這個例子中,test_suite 測試套件包含兩個部分。它們的定義被另一個測試用例分開。 實際上,這些部分可以位於不同的測試文件中。結果的測試樹仍然一樣。 在輸入中可以看到,test_case1 和 test_case2 位於相同的測試套件 test_suite 中。

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

BOOST_AUTO_TEST_SUITE( test_suite )

BOOST_AUTO_TEST_CASE( test_case1 )
{
    BOOST_ERROR( "some error 1" );
}


BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_CASE( test_case_on_file_scope )
{
    BOOST_CHECK( true );
}

BOOST_AUTO_TEST_SUITE( test_suite )

BOOST_AUTO_TEST_CASE( test_case2 )
{
    BOOST_ERROR( "some error 2" );
}

BOOST_AUTO_TEST_SUITE_END()
Source code | Show output
>example --report_level=detailed
Running 3 test cases...
test.cpp(8): error in "test_case1": some error 1
test.cpp(23): error in "test_case2": some error 2

Test suite "example" failed with:
  1 assertion out of 3 passed
  2 assertions out of 3 failed
  1 test case out of 3 passed
  2 test cases out of 3 failed

  Test suite "test_suite" failed with:
    2 assertions out of 2 failed
    2 test cases out of 2 failed

    Test case "test_case1" failed with:
      1 assertion out of 1 failed

    Test case "test_case2" failed with:
      1 assertion out of 1 failed

  Test case "test_case_on_file_scope" passed with:
    1 assertion out of 1 passed


PrevUpHomeNext