2012-05-13 72 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

相关问题