简体   繁体   English

禁止来自Google测试断言的输出

[英]Suppress output from google test assertions

I have a couple of tests that test interpolations of continuous functions, and when my implementation fails for some reason I get lots of output like the following from my Google test suite: 我有两个测试连续函数插值的测试,当我的实现由于某种原因失败时,我从Google测试套件中得到了很多输出,如下所示:

/home/tlycken/exjobb/Code/alpha-orbit-follower/test/interpolation/interpolation-tests.cpp:71: Failure
The difference between this->test_function_y_deriv(x,y) and this->getInterpObjectPtr()->evaluateAt(x,y,0,1) is 1.5395837072062037, which exceeds tol, where
this->test_function_y_deriv(x,y) evaluates to -1.5395837072062037,
this->getInterpObjectPtr()->evaluateAt(x,y,0,1) evaluates to 0, and
tol evaluates to 0.01.

Since I basically loop over the interpolated interval and test with ASSERT_NEAR() in a lot of places, I'm not surprised, but it would be nice to suppress the details of each error message, and just see the pass/fail status of the test case in the report. 由于我基本上遍历了插值间隔并在很多地方使用ASSERT_NEAR()进行测试, ASSERT_NEAR()我并不感到惊讶,但是最好取消显示每个错误消息的详细信息,并仅查看通过/失败状态。报告中的测试用例。 If a test fails, and I don't understand why, I can reenable the output and look closer. 如果测试失败,但我不明白为什么,可以重新启用输出并仔细观察。

However, I can't find any information on how to suppress that kind of output. 但是,我找不到有关如何抑制这种输出的任何信息。 Perhaps my Google-fu is inadequate... 也许我的Google-fu不够用...

Is there a command line flag, or some other means, to suppress the output from individual ASSERT* calls, and just show the final test report? 是否有命令行标志或其他某种方式来禁止单个ASSERT*调用的输出,并仅显示最终的测试报告?

Code sample: 代码示例:

As this seems to be difficult to reproduce, this is the code I'm running. 由于这似乎很难重现,因此这是我正在运行的代码。 In my test fixture, I have the following method: 在我的测试装置中,我有以下方法:

void on_interpolated_grid(std::function<void(double)> f) {
    double dxp = dx / 10;
    double xmax = xmin + (N - 1) * dx;

    for (double xp = xmin + 2 * dx; xp < xmax - 2 * dx; xp += dxp) {
        f(xp);
    }
}

and the test is defined like this: 测试定义如下:

TYPED_TEST(Interp1D_F, SecondOrderDerivative) {
    Interp1D itp(this->N, this->x, this->f);

    this->on_interpolated_grid([itp](double x) -> void {
        ASSERT_NEAR(-sin(x), itp.evaluateAt(x, 2), 1e-3);
    });
}

The members N , x and f on the test fixture are just parameters for initialization of the interpolation object. 测试夹具上的成员Nxf只是用于初始化插值对象的参数。

I also tried to wrap the call to on_interpolated_grid in ASSERT_NO_FATAL_FAILURES , but it didn't help. 我还尝试on_interpolated_grid ASSERT_NO_FATAL_FAILURES的调用包装到on_interpolated_grid中,但这没有帮助。

As far as I can see, the problem results from the fact that ASSERT_NEAR behaves more like EXPECT_NEAR in this case. 据我所知,问题是EXPECT_NEAR在这种情况下ASSERT_NEAR行为更像EXPECT_NEAR

The reason for this behavior is that Google Test uses return but no exceptions in ASSERT_* . 出现这种情况的原因是Google Test在ASSERT_*使用return但没有异常 See their FAQ for why they do this (in short: it allows to run tests even with exceptions disabled but has other advantages in some cases like ASSERT in try -blocks. 请参阅他们的常见问题解答,以了解为什么要这样做(简而言之:即使禁用了例外,它也可以运行测试,但是在某些情况下,例如try -blocks中的ASSERT ,它还有其他优点。

The downside of this approach is exactly what you have here: ASSERT_ does not propagate fatal failures if used in subfunctions (or a lambda as in your case). 这种方法的缺点就是您所拥有的: ASSERT_如果在子函数(或您的情况下为lambda)中使用,则不会传播致命故障。 The GTest Advanced topics page has a section regarding this problem . GTest高级主题页面上有关于此问题的部分 With HasFatalFailure() your situation can be solved with a modified lambda method: 使用HasFatalFailure()可以通过修改的lambda方法解决您的情况:

#include "gtest/gtest.h"
#include "gtest/gtest-typed-test.h"
#include <functional>

void on_interpolated_grid(std::function<void(double)> f) {
    ///ADDED a few values here
    double dxp = 1.0 / 10.0;
    double xmin = 0.1;
    double dx = 0.1;
    int N = 100;
    double xmax = xmin + (N - 1) * dx;

    for (double xp = xmin + 2 * dx; xp < xmax - 2 * dx; xp += dxp) {
        f(xp);
    }
}



TEST(Interp1D_F, SecondOrderDerivative) {
    ///REMOVED. Not required for sample
    //Interp1D itp(this->N, this->x, this->f);

    on_interpolated_grid([](double x) -> void {
        ///ADDED:
        //Only check for further failures if no previous fatal failure ocurred
        if (!this->HasFatalFailure())
            ASSERT_NEAR(-sin(x),0 , 1e-3);

    });
}

To see the full test output (if desired), you just remove the added line. 要查看完整的测试输出(如果需要),只需删除添加的行。

Recently I asked similar question ( Reducing output of gtest, to look similar to output of cxxtest ). 最近,我提出了类似的问题( 减少gtest的输出,使其看起来类似于cxxtest的输出 )。

If you want to fully suppress the output, you can use ::testing::EmptyTestEventListener . 如果要完全抑制输出,可以使用::testing::EmptyTestEventListener For anything fancy, you have to implement custom listener (see for example here ). 出于任何幻想,您必须实现自定义侦听器(例如参见此处 )。

You can even add an option in main() to change the listener depending on program parameters. 您甚至可以在main()添加一个选项,以根据程序参数更改侦听器。

You said that you want colorful output. 您说过要彩色输出。 googletest's maintainers added functions to print text in color. googletest的维护者增加了以彩色文本打印功能。 I know you shouldn't, but if you declare those functions, you will get access to them. 我知道您不应该这样做,但是如果您声明这些功能,则可以访问它们。

It seems google test AssertionResult are printed to standard output by design. 看来Google测试AssertionResult是按设计打印到标准输出的。 No built-in command line option to disable specific parts of the that output either. 没有内置的命令行选项来禁用该输出的特定部分。

If tweaking google test itself is an option, then the easiest would probably be to add a condition around the PrintTestPartResult call in PrettyUnitTestResultPrinter::OnTestPartResult , and similarly with calls to PrintFullTestCommentIfPresent in OnTestEnd / PrintFailedTests . 如果调整谷歌测试本身是一个选项,那么最简单的很可能是周围添加一个条件PrintTestPartResult呼叫PrettyUnitTestResultPrinter::OnTestPartResult ,同样通过调用PrintFullTestCommentIfPresentOnTestEnd / PrintFailedTests In that case, your own option to control verbosity could be included in ParseGoogleTestFlagsOnlyImpl and kColorEncodedHelpMessage . 在这种情况下, ParseGoogleTestFlagsOnlyImplkColorEncodedHelpMessage可以包含您自己的控制详细程度的选项。

Otherwise (ie if recompiling google test is not an option), it is possible to write a TestEventListener replacement to the default PrettyUnitResultPrinter (possibly inspired from PrettyUnitResultPrinter itself), as documented in google test samples . 否则(即,如果没有重新编译google test的选择),可以将TestEventListener替换写入默认的PrettyUnitResultPrinter (可能是从PrettyUnitResultPrinter本身得到的),如google test sample中所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM