在Boost.Test中,如何获取当前测试的名称?
问题描述:
在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的”。
答
有一个可能为此目的调用的未公开的*函数。下面一行将刷新当前测试的名称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)。
答
A different question about suite names提供了一种方式来提取名称,而不是仅仅打印出来:
auto test_name = std::string(boost::unit_test::framework::current_test_case().p_name)
看看[这里](https://groups.google.com/forum/?fromgroups=#!topic/boost-list/ZzFmu14UfeQ),到目前为止它适用于我 –