2012-05-13 79 views
8

Boost.Test中,我如何獲取當前自動測試用例的名稱?在Boost.Test中,如何獲取當前測試的名稱?

實施例:

#include <boost/test/unit_test.hpp> 
BOOST_AUTO_TEST_CASE(MyTest) 
{ 
    std::cerr << "Starting " << test_name << std::endl; 
    // lots of code here 
    std::cerr << "Ending " << test_name << std::endl; 
} 

在示例中,我希望變量test_name含有 「MyTest的」。

+1

看看[這裏](https://groups.google.com/forum/?fromgroups=#!topic/boost-list/ZzFmu14UfeQ),到目前爲止它適用於我 –

回答

17

有一個可能爲此目的調用的未公開的*函數。下面一行將刷新當前測試的名稱cerr

#include <boost/test/framework.hpp> 

... 

std::cerr << boost::unit_test::framework::current_test_case().p_name 
      << std::endl; 

不過請注意,使用此API不會在參數化的測試情況下刷新參數。

您可能也有興趣在test checkpoints **(這似乎是你想要做什麼。)

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

... 

BOOST_AUTO_TEST_CASE(MyTest) 
{ 
    BOOST_TEST_CHECKPOINT("Starting"); 
    // lots of code here 
    BOOST_TEST_CHECKPOINT("Ending"); 
} 

編輯

*的current_test_case()功能現在證明,見the official Boost documentation

** BOOST_TEST_CHECKPOINT以前被稱爲BOOST_CHECKPOINT。請參閱Boost changelog (1.35.0)

+0

但是,當我使用--run_test = <打印名稱>時,我不會使用該名稱,只能使用通配符運行我的測試,我不能得到確切的測試名稱,奇怪! – Antonio

+1

解決了,我不得不指定測試套件,並執行'--run_test = /<打印名稱>'! – Antonio

+0

我添加了文檔+缺少包含的鏈接。 – BenC

相關問題