首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Boost.Test源定位格式的控制输出

Boost.Test源定位格式的控制输出
EN

Stack Overflow用户
提问于 2020-10-31 05:54:23
回答 1查看 204关注 0票数 0

Catch2Boost.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的典型输出

代码语言:javascript
复制
----------------------------------------------
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的典型输出

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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更新):

代码语言:javascript
复制
#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格式)。

代码语言:javascript
复制
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):

代码语言:javascript
复制
#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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64618840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档