Catch2和Boost.Test为编写单元测试提供了类似的特性。对于某个项目,我必须使用Boost.Test而不是Catch2。我遇到的问题是,两者都使用不同的格式输出。
例如,Catch2会说是失败的
test.cpp:9
(见下文示例)。但是Boost.Test会说
test.cpp(9): error in ...。
这种格式不允许我的编辑器将输出识别为源位置。
有一种使file.ext:lineno file.ext(lineno)**?**而不是file.ext(lineno)**?**使Boost.Test输出源位置的方法
这是Catch2的典型输出
----------------------------------------------
Testing Binary Search
----------------------------------------------
test.cpp:9
..............................................test.cpp:18: FAILED:
REQUIRE( binary_search(arr, 176) == 0 )
with expansion:
-1 == 0==============================================
test cases: 1 | 1 failed
assertions: 5 | 4 passed | 1 failed这是Boost.Test的典型输出
Running 7 test cases...
./layout.hpp(764): error: in "layout_to_offset_1d_nontrivial": check L[3] == &B[3] - base(B) has failed [3 != 6]
Running 7 test cases...
./.././detail/layout.hpp(764): error: in "layout_to_offset_1d_nontrivial": check L[3] == &B[3] - base(B) has failed [3 != 6]
*** 1 failure is detected in the test module "C++ Unit Tests for Multi layout"发布于 2020-10-31 06:57:57
我在这篇历史文章中找到了一个解决方案:https://richarddingwall.name/2008/06/01/using-the-boost-unit-test-framework-with-xcode-3
使用虚拟函数覆盖和Boost.Test装置的艺术(丢失):
只需添加以下代码(将一些更新添加到原始文章中,包含次要格式和C++11更新):
#include<boost/test/output/compiler_log_formatter.hpp>
struct xcode_log_formatter: boost::unit_test::output::compiler_log_formatter{
// Produces an Xcode-friendly message prefix.
void print_prefix(std::ostream& output, boost::unit_test::const_string file_name, std::size_t line) override{
output << file_name << ':' << line << ": error: ";
}
};
// Set up the unit test framework to use an xcode-friendly log formatter.
struct xcode_config{
xcode_config(){boost::unit_test::unit_test_log.set_formatter(new xcode_log_formatter);}
};
// Call our fixture.
BOOST_GLOBAL_FIXTURE(xcode_config);通过这一更改,输出看起来像(注意文件:lineno格式)。
Running 7 test cases...
./layout.hpp:781: error: error: in "layout_to_offset_1d_nontrivial": check L[3] == &B[3] - base(B) has failed [3 != 6]
Running 7 test cases...
./.././detail/layout.hpp:781: error: error: in "layout_to_offset_1d_nontrivial": check L[3] == &B[3] - base(B) has failed [3 != 6]我仍然对更简单的解决方案感兴趣。
这里是这个代码的一个更紧凑的版本,根据我自己的情况重命名(xcode->gedit):
#include<boost/test/output/compiler_log_formatter.hpp>
struct gedit_config{
struct formatter : boost::unit_test::output::compiler_log_formatter{
void print_prefix(std::ostream& out, boost::unit_test::const_string file, std::size_t line){
out<< file <<':'<< line <<": ";
}
};
gedit_config(){boost::unit_test::unit_test_log.set_formatter(new formatter);}
};
BOOST_GLOBAL_FIXTURE(gedit_config);https://stackoverflow.com/questions/64618840
复制相似问题