配置 gtest 以仅在控制台中显示失败的测试

发布于 2024-11-19 08:03:10 字数 47 浏览 2 评论 0 原文

是否可以选择仅显示失败的测试?我不得不改用吉他来实现这一点,但我想念命令行工具。

Is there an option to show only failed tests? I had to switch to use Guitar to achieve this, but I miss command line tool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烏雲後面有陽光 2024-11-26 08:03:10

我遇到了同样的问题 - 我相信很多其他人也遇到过。所以我创建了这个:

https://gist.github.com/elliotchance/8215283

应该很漂亮大量粘贴和播放。

I ran into the same issue - as I'm sure many other people have. So I created this:

https://gist.github.com/elliotchance/8215283

Should be pretty much paste and play.

椒妓 2024-11-26 08:03:10

现在有一个内置的解决方案:

your_test_binary --gtest_brief=1

另请参阅 文档。还有 GTEST_BRIEF 环境变量来配置它。

There is a built-in solution for this now:

your_test_binary --gtest_brief=1

See also the documentation. There is also the GTEST_BRIEF environment variable to configure this.

风铃鹿 2024-11-26 08:03:10

有两种方法可以实现这一目标。

第一个是编写自己的事件侦听器:

https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#defining-event-listeners

另一种方法是过滤 googletest 事件监听器的输入收到。

对于这种方法,您删除当前的事件侦听器并将其与您自己的事件侦听器交换

testing::TestEventListeners& listeners = testing::UnitTest::GetInstance()->listeners();
testing::TestEventListener* listener = listeners.Release(listeners.default_result_printer());
listeners.Append(new FailurePrinter(listener));

,其中 FailurePrinter 是您自己的事件侦听器类。

这个类应该看起来像这样

class FailurePrinter : public ::testing::TestEventListener {

public:
FailurePrinter(TestEventListener* listener) : TestEventListener() {_listener = listener;}

virtual void OnTestProgramStart(const UnitTest& unit_test);
virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);
virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);
virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test);
virtual void OnTestCaseStart(const TestCase& test_case);
virtual void OnTestStart(const TestInfo& test_info);
virtual void OnTestPartResult(const TestPartResult& result);
virtual void OnTestEnd(const TestInfo& test_info);
virtual void OnTestCaseEnd(const TestCase& test_case);
virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test);
virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test);
virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
virtual void OnTestProgramEnd(const UnitTest& unit_test);

protected:
testing::TestEventListener* _listener;
};

现在您必须实现所有方法。

如果您喜欢 google 事件监听器打印某些内容的方式,只需将调用委托给 _listener 即可。

或者您可以修改结果。
例如:

void FailurePrinter::OnTestPartResult(const TestPartResult& test_part_result)
{
  if (test_part_result.failed())
  {
      _listener->OnTestPartResult(test_part_result);
      printf("\n");
  }
}

只会打印 Testfailures。

There are two ways to achieve this.

first one is to write your own event listener:

https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#defining-event-listeners

Another way is to filter the input the googletest event listener receives.

For this approache you remove the current event listener and exchange it with your own

testing::TestEventListeners& listeners = testing::UnitTest::GetInstance()->listeners();
testing::TestEventListener* listener = listeners.Release(listeners.default_result_printer());
listeners.Append(new FailurePrinter(listener));

where FailurePrinter is your own event listener class.

This class should look like this

class FailurePrinter : public ::testing::TestEventListener {

public:
FailurePrinter(TestEventListener* listener) : TestEventListener() {_listener = listener;}

virtual void OnTestProgramStart(const UnitTest& unit_test);
virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration);
virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test);
virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test);
virtual void OnTestCaseStart(const TestCase& test_case);
virtual void OnTestStart(const TestInfo& test_info);
virtual void OnTestPartResult(const TestPartResult& result);
virtual void OnTestEnd(const TestInfo& test_info);
virtual void OnTestCaseEnd(const TestCase& test_case);
virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test);
virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test);
virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration);
virtual void OnTestProgramEnd(const UnitTest& unit_test);

protected:
testing::TestEventListener* _listener;
};

Now you have to implement all the methods.

If you like the way googles event listener prints something, just delegate the call to the _listener.

Or you can modify the result.
For example:

void FailurePrinter::OnTestPartResult(const TestPartResult& test_part_result)
{
  if (test_part_result.failed())
  {
      _listener->OnTestPartResult(test_part_result);
      printf("\n");
  }
}

will only print Testfailures.

傲娇萝莉攻 2024-11-26 08:03:10

我编写了 Google Test Pretty Printer,这是一个用于 Google Test 的测试侦听器/漂亮打印机,以提供更清洁和更多功能对于 Google 测试程序来说有吸引力的控制台输出。它包含一个 --failures-only 选项,应该可以完成您想要的操作。

I wrote Google Test Pretty Printer, a test listener / pretty printer for Google Test, to provide cleaner and more attractive console output for Google Test programs. It includes a --failures-only option that should do what you want.

放血 2024-11-26 08:03:10

如果您想要一个快速而肮脏的 Python 2/3 解决方案,仅用于失败的测试,没有外部依赖项: https://gist.github.com/DTasev/a894e4727eeaa94541d90ea1a3cc71a7。它将显示失败的测试及其输出。文件顶部文档字符串中的使用说明

它需要 gtest 的默认输出,因此如果您进行了更改,它将无法工作。

If you want a quick and dirty Python 2/3 solution for only failed tests, with no external dependencies: https://gist.github.com/DTasev/a894e4727eeaa94541d90ea1a3cc71a7. It will show failed test + its output. Instruction to use in docstring at the top of file

It requires gtest's default output, so if you've changed that it won't work.

蛮可爱 2024-11-26 08:03:10

根据文档,您可以使用测试事件更改输出。看这里(还有一个示例): https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#extending-googletest-by-handling-test-events

According to the documentation you can change output using Test Events. Look here (there is also an example): https://github.com/google/googletest/blob/master/googletest/docs/advanced.md#extending-googletest-by-handling-test-events

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文