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

Manually registered test case template

One way to perform the same set of checks for a component instantiated with different template parameters is illustrated in the following example:
對用不同模板參數實例化的組件執行相同檢查集合的一種方法是像下面例子這樣:

template <typename T>
void single_test()
{
    BOOST_CHECK( /* test assertion */ );
}

void combined_test()
{
    single_test<int>();
    single_test<float>();
    single_test<unsigned char>();
}

There several problems/inconveniencies with above approach, including:
上面的方法存在一些問題 / 不方便,包括:

Ideally the test case template would be based on nullary function template (like single_test above). Unfortunately function templates are neither addressable nor can be used as template parameters. To alleviate the issue the manually registered test case template facility consists of two co-working macros: BOOST_TEST_CASE_TEMPLATE_FUNCTION and BOOST_TEST_CASE_TEMPLATE. Former is used to define the test case template body, later - to create and register test cases based on it.
理想情況下,測試用例模板應用基於無參函數模板 (像上面的 single_test)。 然而不幸的是,函數模板既不能取地址,也不能被當作模板參數。 為了減輕這個麻煩,手動註冊測試用例模板工具提供兩個合作的宏: BOOST_TEST_CASE_TEMPLATE_FUNCTION 和 BOOST_TEST_CASE_TEMPLATE。 前者用於定義測試用例模板主體,後者用於基於前兩創建並註冊測試用例。

The macro BOOST_TEST_CASE_TEMPLATE_FUNCTION requires two arguments: the name of the test case template and the name of the format type parameter:
BOOST_TEST_CASE_TEMPLATE_FUNCTION 宏需要兩個參數:測試用例模板的名稱和參數類型的形式名稱:

BOOST_TEST_CASE_TEMPLATE_FUNCTION(test_case_name, type_name)
BOOST_TEST_CASE_TEMPLATE_FUNCTION( test_case_name, type_name )
{
  // test case template body
}

The macro BOOST_TEST_CASE_TEMPLATE_FUNCTION is intended to be used in place of nullary function template signature:
BOOST_TEST_CASE_TEMPLATE_FUNCTION 宏產生如下的無參函數模板簽名:

template<typename type_name>
void
test_case_name()
{
  // test case template body
}

The only difference is that the BOOST_TEST_CASE_TEMPLATE_FUNCTION makes the test case template name usable in the template argument list.
唯一的區別在於 BOOST_TEST_CASE_TEMPLATE_FUNCTION 使得測試用例模板名稱可在模板參數列表中使用。

BOOST_TEST_CASE_TEMPLATE requires two arguments: the name of the test case template and Boost.MPL compatible collection of types to instantiate it with. The names passed to both macros should be the same.
BOOST_TEST_CASE_TEMPLATE 需要兩個參數:測試用例模板名稱和用於實例化的兼容 Boost.MPL 的類型集合。 傳入兩個宏的名稱應該一樣。

BOOST_TEST_CASE_TEMPLATE(test_case_name, collection_of_types)

BOOST_TEST_CASE_TEMPLATE creates an instance of the test case generator. When passed to the method test_suite::add, the generator produces a separate sub test case for each type in the supplied collection of types and registers it immediately in the test suite. Each test case is based on the test case template body instantiated with a particular test type.
BOOST_TEST_CASE_TEMPLATE 創建一個測試用例產生器實例。 被傳入到 test_suite::add 方法時,產生器為提供的類型集合內每個類型產生單獨的子測試用例,並直接註冊到測試套件中。 每個測試用例都是基於特定測試類型實例化的測試用例模板主體。

Sub test case names are deduced from the macro argument test_case_name. If you prefer to assign different test case names, you need to use the underlying make_test_case interface instead. Both test cases creation and registration is performed in the test module initialization function.
子測試用例名稱通過宏參數 test_case_name 推斷。 如果想要指定不同的測試用例名稱,需要使用底層的 make_test_case 接口。 測試用例創建和註冊都在測試模塊初始化函數中完成。

The test case template facility is preferable to the approach in example above, since execution of each sub test case is guarded and counted separately. It produces a better test log/results report (in example above in case of failure you can't say which type is at fault) and allows you to test all types even if one of them causes termination of the sub test case.
測試用例模板工具相對上面的示例更好一些,因為每個子測試用例的執行都是獨立監控和計數的。 它產生更好的測試日誌 / 結果報告 (在上面的例子中如果失敗是沒有辦法知道哪個類型失敗的),並允許測試所有類型即使某個子測試用例導致中斷。

Example 13. Manually registered test case template

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

//____________________________________________________________________________//

BOOST_TEST_CASE_TEMPLATE_FUNCTION( my_test, T )
{
    BOOST_CHECK_EQUAL( sizeof(T), 4 );
}

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] )
{
    typedef boost::mpl::list<int,long,unsigned char> test_types;

    framework::master_test_suite().
        add( BOOST_TEST_CASE_TEMPLATE( my_test, test_types ) );

    return 0;
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 3 test cases...
test.cpp(10): error in "my_test<unsigned char>": check sizeof(T) == 4 failed [1 != 4]

*** 1 failure detected in test suite "Master Test Suite"


PrevUpHomeNext