Home > The Unit Test Framework > User's guide > Supplied test runners >
PrevNext

The supplied test runners or where is the entrance?

All usage variants of the UTF, excluding the external test runner, supply the test runner in a form of free function named unit_test_main with the following signature:
UTF 的所有用法,除了外部測試運行器, 都以名為 unit_test_main 的自由函數形式提供了測試運行器,簽名如下:

int unit_test_main( init_unit_test_func init_func, int argc, char* argv[] );

To invoke the test runner you are required to supply the pointer to the test module initialization function as the first argument to the test runner function. In majority of the cases this function is invoked directly from test executable entry point - function main(). In most usage variants the UTF can automatically generate default function main() implementation as either part of the library or test module itself. Since the function main needs to refer to the initialization function by name, it is predefined by the default implementation and you are required to match both specific signature and name, when implementing initialization function. If you for any reason prefer more flexibility you can opt to implement the function main() yourself, in which case it's going to be your responsibility to invoke the test runner, but the initialization function name is not enforces the UTF. See below for flags that needs to be defined/undefined in each usage variant to enable this.
要調用測試運行器需要將指向測試模塊初始化函數的指針作為第一個參數提供給測試運行器函數。 大多數情況下,這個函數是由測試運行入口點 - 函數 main() 調用的。 在大多數 UTF 用法中,默認的函數 main() 實現都是可以作為庫或測試模塊的一部分自動生成的。 因為 main 函數需要根據默認實現定義好的名稱引用初始化函數,所以你在實現初始化函數時需要符合特定的簽名和名稱。 如果因為某些原因希望更多的可伸縮性,你可以自己實現 main() 函數,在這種情況下你需要自己調用測試運行器,但初始化函數名稱就不是 UTF 強制的了。 下面的部分說明了在不同用法中這樣做需要定義 / 取消定義的標籤。

[Warning] Warning

In spite syntactic similarity the signatures of the test runner function in fact are different for different usage variants. The cause is different signature of the test module initialization function referred by the typedef init_unit_test_func. This makes static and dynamic library usage variants incompatible and they can't be easily switched on a fly.
雖然測試運行器函數的簽名在語法上類似,但實際上是用於不同用法的。 這是因為不同的測試模塊初始化函數是由 typedef init_unit_test_func 引用的。(???) 這使得靜態庫和動態庫不兼容,並且不是很容易進行切換。

Static library variant of the UTF

By default this variant supplies the function main() as part of static library. If this is for any reason undesirable you need to define the flag BOOST_TEST_NO_MAIN during the library compilation and the function main() implementation won't be generated.
這種用法默認提供 main() 函數作為靜態庫的一部分。 無論什麼原因,如果這不是你想要的,你需要在編譯庫的過程中定義標籤 BOOST_TEST_NO_MAIN,這樣 main() 函數的實現就不會被生成了。

In addition to the initialization function signature requirement default function main() implementation assumes the name of initialization function is init_unit_test_suite
另外,默認 main() 函數實現的初始化函數簽名需求假設初始化函數名稱為 init_unit_test_suite

Dynamic library variant of the UTF

Unlike the static library variant function main() can't reside in the dynamic library body. Instead this variant supplies default function main() implementation as part of the header boost/test/unit_test.hpp to be generated as part of your test file body. The function main() is generated only if either the BOOST_TEST_MAIN or the BOOST_TEST_MODULE flags are defined during a test module compilation. For single-file test module flags can be defined either in a test module's makefile or before the header boost/test/unit_test.hpp inclusion. For a multi-file test module flags can't be defined in makefile and have to be defined in only one of the test files to avoid duplicate copies of the function main().
不像靜態庫用法,main() 函數不能位於動態庫內部。於是,這種方法將 main() 函數的默認實現作為頭文件 boost/test/unit_test.hpp 的一部分,使其出現在你的測試文件體中。 只有在測試模塊被編譯時定義標籤 BOOST_TEST_MAINBOOST_TEST_MODULE,main() 函數才會被生成。 對於單文件測試模塊,標籤可以定義在測試模塊的 makefile 文件中或包含頭文件 boost/test/unit_test.hpp 之前。 而對於多文件測試模塊,標籤不能定義在 makefile 中,而只能定義在某一個測試文件中,避免 main() 函數的多重拷貝出現。

[Important] Important

The same flags also govern generation of an empty test module initialization function. This means that if you need to implement either function main() or initialization function manually, you can't define the above flags and are required to manually implement both of them.
同樣的標籤同樣會控制空的測試模塊初始化函數。 這意味著如果你想要自己實現 main() 函數或初始化函數,你就不能定義上面的標籤,並且需要手動實現兩者。

Single-header variant of the UTF

By default this variant supplies function main() as part of the header boost/test/included/unit_test.hpp to be generated as part of your test file body. If this is for any reason undesirable you need to define the flag BOOST_TEST_NO_MAIN during test module compilation and the function main() implementation won't be generated.
默認情況下,這種用法將 main() 函數作為頭文件 boost/test/included/unit_test.hpp 的一部分成為你測試文件體的一部分。 如果這不是你想要的,你需要在編譯測試模塊時定義標籤 BOOST_TEST_NO_MAIN,這樣就不會生成 main() 函數的實現了。

Generated exit status values

Once testing is finished, all supplied test runners report the results and returns an exit status value. Here are the summary of all possible generated values:
一旦測試完成,所有提供的測試運行器都報告結果並返回退出狀態值。 下面是所有可能產生值的總結:

Table 4. Generated exit status values

Value Meaning
boost::exit_success No errors occurred during the test or the success result code was explicitly requested with the no_result_code parameter.
測試中沒有錯誤發生,或者 no_result_code 參數明確要求成功結果代碼。
boost::exit_test_failure Non-fatal errors detected and no uncaught exceptions were thrown during testing or the UTF fails during initialization.
檢查到非致命錯誤並且測試中沒有拋出未捕獲的異常,或者 UTF 初始化失敗。
boost::exit_exception_failure Fatal errors were detected or uncaught exceptions thrown during testing.
檢查到致命錯誤,或者測試中拋出未捕獲的異常。


PrevUpHomeNext