我编写了一些脚本来自动运行我们的单元测试,这些脚本是使用 boost 单元测试框架编写的。我想添加功能以允许选择和随后运行所有测试的子集。我知道我可以使用 run_test 参数运行测试子集,但是我找不到列出编译二进制文件中的所有测试的方法,即我可以传递给 run_test 的所有参数值。有没有办法提取所有可用的测试,还是我必须编写一个自定义测试运行器?如果是这样,我从哪里开始?
问问题
2772 次
2 回答
4
boost::test 内部的文档可能有点缺乏,也就是说一切都可用。
查看 boost::test 头文件,特别是 test_suite 和 test_unit 类。有一个名为 traverse_test_tree 的函数可用于遍历已注册的测试。
下面是我编写的一些示例代码,用于以特定格式输出测试结果,它使用 traverse_test_tree 输出每个测试的结果,希望它能给你一个良好的开端......
/**
* Boost test output formatter to output test results in a format that
* mimics cpp unit.
*/
class CppUnitOpFormatter : public boost::unit_test::output::plain_report_formatter
{
public:
/**
* Overidden to provide output that is compatible with cpp unit.
*
* \param tu the top level test unit.
* \param ostr the output stream
*/
virtual void do_confirmation_report( boost::unit_test::test_unit const& tu,
std::ostream& ostr );
};
class CppUnitSuiteVisitor : public test_tree_visitor
{
public:
explicit CppUnitSuiteVisitor( const string& name ) : name_( name )
{}
virtual void visit( const test_case& tu )
{
const test_results& tr = results_collector.results( tu.p_id );
cout << name_ << "::" << tu.p_name << " : " << ( tr.passed() ? "OK\n" : "FAIL\n" );
}
private:
string name_;
};
// ---------------------------------------------------------------------------|
void CppUnitOpFormatter::do_confirmation_report(
test_unit const& tu, std::ostream& ostr )
{
using boost::unit_test::output::plain_report_formatter;
CppUnitSuiteVisitor visitor( tu.p_name );
traverse_test_tree( tu, visitor );
const test_results& tr = results_collector.results( tu.p_id );
if( tr.passed() )
{
ostr << "Test Passed\n";
}
else
{
plain_report_formatter::do_confirmation_report( tu, ostr );
}
}
于 2011-12-19T11:57:16.667 回答
2
Boost.Test 的主干版本有命令行参数来获得你需要的东西。
于 2012-05-24T23:02:26.513 回答