Home > The Unit Test Framework > User's guide > Test organization > Nullary function based test case > Manual registration
PrevNext

Manually registered nullary function based test case
手動註冊基於無參函數的測試用例

To create a test case manually, employ the macro BOOST_TEST_CASE:
要手動創建測試散例,可以使用宏 BOOST_TEST_CASE:

BOOST_TEST_CASE(test_function)

BOOST_TEST_CASE creates an instance of the class boost::unit_test::test_case and returns a pointer to the constructed instance. The test case name is deduced from the macro argument test_function. If you prefer to assign a different test case name, you have to use the underlying make_test_case interface instead. To register a new test case, employ the method test_suite::add. Both test case creation and registration are performed in the test module initialization function.
BOOST_TEST_CASE 創建一個 boost::unit_test::test_case 類的實例,並返回指向這個實例的指針。 測試用例的名稱是從宏的參數 test_function 推斷而來。 如果你想要使用不同的測試用例名稱,需要使用下面的 make_test_case 接口。 要註冊一個新的測試用例,使用方法 test_suite::add。 測試用例的創建和註冊都是在測試模塊的初始化函數。

Here is the simplest possible manually registered test case. This example employs the original test module initialization function specification. A single test case is created and registered in the master test suite. Note that the free function name is passed by address to the macro BOOST_TEST_CASE.
這可能是最簡單的手動註冊的測試用例。這個示例使用了原始的測試模塊初始化定義。 單個測試用例被創建並註冊到主測試套件。 自由函數的名稱是通過宏 BOOST_TEST_CASE 傳入的。

Example 5. Nullary free function manually registered

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

//____________________________________________________________________________//

void free_test_function()
{
    BOOST_CHECK( true /* test assertion */ );
}

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    framework::master_test_suite().
        add( BOOST_TEST_CASE( &free_test_function ) );

    return 0;
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...

*** No errors detected

A test case can be implemented as a method of a class. In this case a pointer to the class instance has to be bound to the test method to create a test case. You can use the same instance of the class for multiple test cases. The UTF doesn't take an ownership of the class instance and you are required to manage the class instance lifetime yourself.
測試用例也可以用類方法實現。在這種情況下,指向類實例的指針需要和測試方法綁定來創建測試用例。 你可以一個類的實例創建多個測試用例。 UTF 並不取得類實例的所有權,所以你需要自己管理類實例的生命週期。

[Warning] Warning

The class instance can't be defined in the initialization function scope, since it becomes invalid as soon as the test execution exits it. It needs to be either defined statically/globally or managed using a shared pointer.
類實例不能定義在初始化函數範圍內,因為在測試執行離開這個範圍時它就會變得無效。 它需要被定義為靜態 / 全局的或使用共享指針管理。

Example 6. Nullary method of a class bound to global class instance and manually registered

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

//____________________________________________________________________________//

class test_class {
public:
    void test_method()
    {
        BOOST_CHECK( true /* test assertion */ );
    }
} tester;

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &test_class::test_method, &tester )));

    return 0;
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...
test.cpp(11): error in "boost::bind( &test_class::test_method, &tester )": check false failed

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

Example 7. Nullary method of a class bound to shared class instance and manually registered

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

//____________________________________________________________________________//

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;
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 2 test cases...
test.cpp(15): error in "boost::bind( &test_class::test_method2, tester )": check false failed

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

If you do not need to reuse the test class instance and can't or do not wish to create test class instance globally it may be easier and safer to create an instance on the stack of free function:
如果你不需要復用測試類的實例,並且不能或不希望創建全局的測試類實例,那麼更好更安全的方法可能是在自由的棧上創建實例。

Example 8. Nullary method of a class bound to local class instance inside free function

#include<boost/test/included/unit_test.hpp>

using namespace boost::unit_test; 

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

//____________________________________________________________________________//

void free_test_function() 
{ 
    test_class inst; 

    inst.test_method(); 
} 

//____________________________________________________________________________//

test_suite* 
init_unit_test_suite( int argc, char* argv[] ) 
{ 
    framework::master_test_suite().add( BOOST_TEST_CASE( &free_test_function ) ); 

    return 0; 
} 

//____________________________________________________________________________//
Source code | Show output
> example
Running 1 test case...

*** No errors detected

If you have to perform the same set of tests with different sets of parameters you may want to base your test case on a function with arguments and bind particular parameters during test case creation.
如果你需要將不同的參數集合應用於相同的測試集合,你可以會將測試用例基於帶參數的函數,並在創建測試用例時綁定特定的參數。

[Warning] Warning

If you bind parameters by reference or pointer, the referenced value can't have local storage in the test module initialization function.
如果通過引用或指針綁定參數,被綁定的數據不能是在測試模塊初始化函數內部存儲的。

Example 9. Binary free function bound to set of different parameter pairs

This example employs the alternative test module initialization function specification.
這個例子使用了備選的測試模塊初始化函數定義。

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

//____________________________________________________________________________//

void free_test_function( int i, int j )
{
    BOOST_CHECK( true /* test assertion */ );
}

//____________________________________________________________________________//

bool
init_function()
{
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &free_test_function, 1, 1 ) ) );
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &free_test_function, 1, 2 ) ) );
    framework::master_test_suite().
        add( BOOST_TEST_CASE( boost::bind( &free_test_function, 2, 1 ) ) );

    return true;
}

//____________________________________________________________________________//

int
main( int argc, char* argv[] )
{
    return ::boost::unit_test::unit_test_main( &init_function, argc, argv );
}

//____________________________________________________________________________//
Source code | Show output
> example
Running 3 test cases...

*** No errors detected

The UTF also presents an alternative method for parameterized test case creation, which is covered in the section called 「Unary function based test case」.
UTF 同樣提供了用於參數化測試用例創建的備選方法,在 「基於無參函數的測試用例」 一節介紹。


PrevUpHomeNext