11

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”。

4

2 回答 2

22

为此目的可以调用一个未记录的*函数。以下行会将当前测试的名称刷新为cerr

#include <boost/test/framework.hpp>

...

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

但是请注意,在参数化测试的情况下,使用此 API 不会刷新参数。

您可能还对测试检查点感兴趣**(这似乎是您想要做的。)

#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()功能现已记录在案,请参阅官方 Boost 文档

**BOOST_TEST_CHECKPOINT以前称为BOOST_CHECKPOINT. 请参阅Boost 更改日志 (1.35.0)

于 2012-10-23T09:23:38.783 回答
1

关于套件名称的另一个问题提供了一种提取名称的方法,而不仅仅是打印它:

auto test_name = std::string(boost::unit_test::framework::current_test_case().p_name)
于 2015-08-04T20:43:30.553 回答