控制 Boost.Test 源位置格式的输出 [英] Controlling output of Boost.Test source location format

查看:27
本文介绍了控制 Boost.Test 源位置格式的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Catch2Boost.Test 为编写单元测试提供了类似的功能.对于某个项目,我必须使用 Boost.Test 而不是 Catch2.我的问题是两者都使用不同的格式输出.

Catch2 and Boost.Test provide similar features for writing unit tests. For a certain project I have to use Boost.Test instead of Catch2. The problem I have is that both use different format outputs.

例如,Catch2 会说这是一个失败

For example, Catch2 will say that the was a fail in

test.cpp:9

(见下面的例子).但是 Boost.Test 会说

(see example below). However Boost.Test will say

test.cpp(9): error in ....

这种格式不允许我的编辑器将输出识别为源位置.

This format doesn't allow my editor to recognize the output as a source location.

有没有办法让 Boost.Test 将源位置输出为 file.ext:lineno 而不是 file.ext(lineno)?

Is there way to make Boost.Test output the source location as file.ext:lineno instead of file.ext(lineno)?

这是 Catch2 的典型输出

This is a typical output for 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 的典型输出

This is a typical output for 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"

推荐答案

我在这篇历史帖子中找到了解决方案:https://richarddingwall.name/2008/06/01/using-the-boost-unit-test-framework-with-xcode-3

I found a solution in this historical post: https://richarddingwall.name/2008/06/01/using-the-boost-unit-test-framework-with-xcode-3

使用(丢失的)虚函数覆盖和 Boost.Test 装置的艺术:

Using the (lost of) art of virtual function override and Boost.Test fixtures:

只需添加此代码(对原始帖子的一些更新,带有次要格式和 C++11 更新):

Simply add this code (some updates to the original post, with minor formats and C++11 updates):

#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 格式).

with this change the output looks like (note file:lineno format).

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]

我仍然对更简单的解决方案感兴趣.

I am still interested in simpler solutions.

这是这段代码的更紧凑版本,根据我自己的情况重命名(xcode->gedit):

Here it is a more compact version of this code, renamed for my own case (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);

这篇关于控制 Boost.Test 源位置格式的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆