使用BOOST.Test在文件中生成测试报告的更好方法是什么? [英] What is the better way to generate test report in a file using BOOST.Test?
本文介绍了使用BOOST.Test在文件中生成测试报告的更好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道默认报告是针对标准错误,因此必须重定向到一个文件。
我的问题是我们在一个全局夹具里这样做吗?
I know by default report is directed to standard-error, and so one has to redirect it to a file. My question is shall we do this inside a global fixture? Which isn't seem to be working for me some how.
这是我试过的 -
struct MyConfig
{
MyConfig()
: testReport("fileName.log")
{
if(!testReport.fail())
original = std::cerr.rdbuf(testReport.rdbuf());
}
~MyConfig()
{
cerr.rdbuf(original);
testReport.close();
}
ofstream testReport;
streambuf* original;
};
BOOST_GLOBAL_FIXTURE(MyConfig);
运行测试后,仅在控制台上报告输出,但使用给定名称创建一个0kb文件。
After running the test, report outputs on console only, though a 0kb file is created with the given name.
推荐答案
您可以尝试此替代方案,修改自这里,并声称工作在Boost 1.34.1。这似乎更像Boost意图 - 查看结果流的使用overrider。
You could try this alternative, adapted from here and alleged to work on Boost 1.34.1. This seems to be more as Boost intends - see the usage of a results stream overrider.
//
// run_tests.cc
//
#define BOOST_AUTO_TEST_MAIN
#include <iostream>
#include <fstream>
#include <cassert>
#include <boost/test/auto_unit_test.hpp>
#include <boost/test/results_reporter.hpp>
std::ofstream ReportRedirector::out;
struct ReportRedirector
{
ReportRedirector()
{
out.open("fileName.log");
assert( out.is_open() );
boost::unit_test::results_reporter::set_stream(out);
}
private:
static std::ofstream out;
};
BOOST_GLOBAL_FIXTURE(ReportRedirector)
这篇关于使用BOOST.Test在文件中生成测试报告的更好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文