【问题标题】:In Boost.Test, how to obtain the name of current test?在 Boost.Test 中,如何获取当前测试的名称?
【发布时间】:2012-05-13 19:03:31
【问题描述】:

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

【问题讨论】:

  • 看看this,到目前为止它对我有用

标签: boost boost-test


【解决方案1】:

有一个未记录的*函数可以为此目的调用。以下行会将当前测试的名称刷新为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)

【讨论】:

  • 但是,当我使用 --run_test= 时,我不打算使用该名称,只能使用通配符运行我的测试,我无法获得确切的测试名称,奇怪!!
  • 已解决,我还必须指定测试套件,然后执行--run_test=&lt;TEST_SUITE&gt;/&lt;THE PRINTED NAME&gt;
  • 我添加了指向文档的链接 + 缺少的包含。
  • 当我在那里的时候,我还修复了BOOST_CHECKPOINT 宏。
  • 谢谢!我也打了这个
【解决方案2】:

A different question about suite names 提供了一种提取名称的方法,而不仅仅是打印它:

auto test_name = std::string(boost::unit_test::framework::current_test_case().p_name)

【讨论】:

    猜你喜欢
    • 2012-09-26
    • 2022-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多