Home > The Unit Test Framework > User's guide > Test organization > Test suite > Master Test Suite
PrevNext

Master Test Suite

As defined in introduction section the master test suite is a root node of a test tree. Each test module built with the UTF always has the master test suite defined. The UTF maintain the master test suite instance internally. All other test units are registered as direct or indirect children of the master test suite.
就像在介紹章節定義的,主測試套件是測試樹的根節點。 UTF 生成的每個測試模塊都定義有主測試套件。 UTF 內部維護主測試套件的實例。 所有其它的測試單元都被註冊為主測試套件的直接或間接的子節點。

namespace boost {
namespace unit_test {
class master_test_suite_t : public test_suite {
public:
   int argc;
   char** argv;
};

} // namespace unit_test

} // namespace boost

To access single instance of the master test suite use the following interface:
要取得主測試套件的單個實例,使用下面的接口:

namespace boost {
namespace unit_test {
namespace framework {

master_test_suite_t& master_test_suite();

} // namespace framework
} // namespace unit_test
} // namespace boost
Command line arguments access interface

Master test suite implemented as an extension to the regular test suite, since it maintains references to the command line arguments passed to the test module. To access the command line arguments use
主測試套件被實現為普通測試套件的擴展,因為它還保存了傳入測試模塊的命令行參數的引用。 要取得命令行參數使用

boost::unit_test::framework::master_test_suite().argc
boost::unit_test::framework::master_test_suite().argv

In below example references to the command line arguments are accessible either as an initialization function parameters or as members of the master test suite. Both references point to the same values. A test module that uses the alternative initialization function specification can only access command line arguments through the master test suite.
在下面的例子中,指向命令行參數的引用可以通過初始化函數參數或主測試套件的成員獲得。 兩個引用都指向相同的值。 使用備選初始化函數簽名的測試模塊只能通過主測試套件取得命令行參數。

[Note] Note

This interface for runtime parameter access is temporary. It's planned to be updated once runtime parameters support is redesigned.
這個取得運行時參數的接口是臨時的。一旦運行時參數支持被重新設計,接口將被更新。

Returning to the free function example, let's modify initialization function to check for absence of any test module arguments.
現在回到自由函數的例子,我們修改初始化函數來檢查測試模塊參數存在與否。

Example 18. Command line access in initialization function

#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[] ) 
{
    if( framework::master_test_suite().argc > 1 )
        return 0;

    framework::master_test_suite().
        add( BOOST_TEST_CASE( &free_test_function ) );

    return 0;
}

//____________________________________________________________________________//

Source code | Show output
> example 1
Test setup error: test tree is empty

Naming

The master test suite is created with default name "Master Test Suite". There are two methods two reset the name to a different value: using the macro BOOST_TEST_MODULE and from within the test module initialization function. Former is used for test modules that don't have the manually implemented initialization function. Following examples illustrate these methods.
主測試套件使用默認名稱 "Mater Test Suite" 創建。有兩種方法來改變名稱: 使用宏 BOOST_TEST_MODULE 或者在測試模塊初始化函數中。 前者用於沒有手動實現初始化函數的測試模塊。 下面的例子展示了這些方法。

Example 19. Naming master test suite using the macro BOOST_TEST_MODULE

If the macro BOOST_TEST_MODULE is defined, the test module initialization function is automatically generated and the macro value becomes the name of the master test suite. The name may include spaces.
如果宏 BOOSOT_TEST_MODULE 被定義, 測試模塊初始化函數被自動生成, 並且宏的值被當作主測試套件的名稱。名稱中可能包含空格。

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE my master test suite name
#include <boost/test/included/unit_test.hpp>

//____________________________________________________________________________//

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

//____________________________________________________________________________//

Source code | Show output
> example --log_level=test_suite
Running 1 test case...
Entering test suite "my master test suite name"
Entering test case "free_test_function"
Leaving test case "free_test_function"; testing time: 1ms
Leaving test suite "my master test suite name"

*** No errors detected


Example 20. Naming master test suite explicitly in the test module initialization function

Without the BOOST_TEST_MAIN and the BOOST_TEST_MODULE flags defined, the test module initialization function has to be manually implemented. The master test suite name can be reset at any point within this function.
不定義 BOOST_TEST_MAINBOOST_TEST_MODULE 標籤, 測試模塊初始化函數需要被手動實現。 主測試套件的名稱可以在這個函數中任意時刻重設。

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

//____________________________________________________________________________//

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

//____________________________________________________________________________//

test_suite*
init_unit_test_suite( int argc, char* argv[] ) 
{
    framework::master_test_suite().p_name.value = "my master test suite name";

    return 0;
}

//____________________________________________________________________________//
Source code | Show output
> example --log_level=test_suite
Running 1 test case...
Entering test suite "my master test suite name"
Entering test case "free_test_function"
Leaving test case "free_test_function"; testing time: 1ms
Leaving test suite "my master test suite name"

*** No errors detected



PrevUpHomeNext