【问题标题】:Controlling output of Boost.Test source location format控制 Boost.Test 源位置格式的输出
【发布时间】:2021-02-13 12:48:12
【问题描述】:

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

例如,Catch2 会说这是失败

test.cpp:9

(参见下面的示例)。 但是 Boost.Test 会说

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

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

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

这是 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"

【问题讨论】:

    标签: c++ unit-testing boost-test output-formatting catch2


    【解决方案1】:

    我在这篇历史帖子中找到了解决方案: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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 2010-11-29
      • 1970-01-01
      相关资源
      最近更新 更多