在 Google 测试中打印附加输出

发布于 2024-12-12 11:13:59 字数 645 浏览 1 评论 0原文

我使用的是 googletest C++ 测试框架。通常,运行测试的文本输出如下所示:

[ RUN      ] MyTest.Fuzz
[       OK ] MyTest.Fuzz (1867 ms)

我想以相同的格式输出一些附加数据,例如:

[ RUN      ] MyTest.Fuzz
[          ] random seed = 1319760587
[       OK ] MyTest.Fuzz (1867 ms)

我发现 在 googletest 文档中记录附加信息,但这似乎只将结构化数据发送到 XML 输出,而不是标准控制台输出。

我可以在单元测试中调用一个 googletest 函数来输出这种格式的文本吗?手动将数据发送到 cout 是可行的,但它不包括通常的彩色输出,因此我必须通过打印 13 个空格或其他内容来显式缩进输出。

I'm using the googletest C++ testing framework. Normally the textual output of running a test looks like this:

[ RUN      ] MyTest.Fuzz
[       OK ] MyTest.Fuzz (1867 ms)

I would like to output some additional data in the same format, for example:

[ RUN      ] MyTest.Fuzz
[          ] random seed = 1319760587
[       OK ] MyTest.Fuzz (1867 ms)

I have found Logging Additional Information in the googletest documentation but that only seems to send structured data to the XML output, not the standard console output.

Is there a googletest function I can call inside my unit test that outputs text in this format? Manually sending data to cout works, but it doesn't include the usual coloured output so I have to explicitly indent the output by printing 13 spaces or whatever.

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

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

发布评论

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

评论(6

听你说爱我 2024-12-19 11:13:59

只需打印到 stderr 即可在默认测试配置中工作。

std::cerr << "[          ] random seed = " << random_seed << std::endl;

Simply printing to stderr will work in the default test configuration.

std::cerr << "[          ] random seed = " << random_seed << std::endl;
玩世 2024-12-19 11:13:59

在 gTest 中打印附加输出也可能是这样的:

  EXPECT_NE(result1, result2)
      << "currentTime: " << formattedTime << std::endl
      << "  addinfo1: " << addinfo1 << std::endl
      << "  addinfo2: " << addinfo2 << std::endl;

std::coutstd::cerr 在 gTest 中不起作用。

Printing additional output in gTest also possible like this:

  EXPECT_NE(result1, result2)
      << "currentTime: " << formattedTime << std::endl
      << "  addinfo1: " << addinfo1 << std::endl
      << "  addinfo2: " << addinfo2 << std::endl;

std::cout, std::cerr will not work within gTest.

聊慰 2024-12-19 11:13:59

您可以为默认打印机 PrettyUnitTestResultPrinter 编写一个包装器来打印测试属性。您可以使用listeners.default_result_printer()获取默认打印机(见下文)。您必须实现 EmptyTestEventListener 并更改方法 PrettyUnitTestResultPrinter::OnTestEnd() (在 gtest.cc 中)并使用结果属性: test_info.result()->GetTestProperty(i) 就像 XML 输出的打印机:

String XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes(
    const TestResult& result) {
  Message attributes;
  for (int i = 0; i < result.test_property_count(); ++i) {
    const TestProperty& property = result.GetTestProperty(i);
    attributes << " " << property.key() << "="
        << "\"" << EscapeXmlAttribute(property.value()) << "\"";
  }
  return attributes.GetString();
}

然后您可以替换测试的默认侦听器,就像在 Google 测试示例

UnitTest& unit_test = *UnitTest::GetInstance();
if (terse_output) {
  TestEventListeners& listeners = unit_test.listeners();
  delete listeners.Release(listeners.default_result_printer());
  listeners.Append(new TersePrinter);
}
int ret_val = RUN_ALL_TESTS();

You could write a wrapper for the default printer PrettyUnitTestResultPrinter to also print out test properties. You can get the default printer with listeners.default_result_printer()(see below). You would have to implement EmptyTestEventListener and change the method PrettyUnitTestResultPrinter::OnTestEnd() (in gtest.cc) and use the results properties: test_info.result()->GetTestProperty(i) like the printer for XML output:

String XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes(
    const TestResult& result) {
  Message attributes;
  for (int i = 0; i < result.test_property_count(); ++i) {
    const TestProperty& property = result.GetTestProperty(i);
    attributes << " " << property.key() << "="
        << "\"" << EscapeXmlAttribute(property.value()) << "\"";
  }
  return attributes.GetString();
}

Then you can replace the default listener for your tests like it's done in the google test sample:

UnitTest& unit_test = *UnitTest::GetInstance();
if (terse_output) {
  TestEventListeners& listeners = unit_test.listeners();
  delete listeners.Release(listeners.default_result_printer());
  listeners.Append(new TersePrinter);
}
int ret_val = RUN_ALL_TESTS();
み零 2024-12-19 11:13:59

不,搜索了标题,没有在中间添加您自己的日志。可能有什么要求。如果需要,您可以在 http://code.google.com/p 记录增强任务/googletest/issues/list

小心。

Nope, searched through the headers and there is nothing about adding your own logs in the middle. Might be something to request. You could log an enhancement task if you want at http://code.google.com/p/googletest/issues/list

Take care.

穿透光 2024-12-19 11:13:59

我刚刚在 linux 中使用了 std::cout 和 ansi 颜色代码,但我相信自从 win 10 周年更新以来这些代码可以在 Windows 中工作。

std::cout << "\033[0;32m" << "[          ] " << "\033[0;0m" 
<< "random seed = " << random_seed << std::endl;

或者只是创建一个头文件和一些 #define 语句并将其包含在我的测试中。我还喜欢设置文本格式以使其更加突出。

#define ANSI_TXT_GRN "\033[0;32m"
#define ANSI_TXT_MGT "\033[0;35m" //Magenta
#define ANSI_TXT_DFT "\033[0;0m" //Console default
#define GTEST_BOX "[     cout ] "
#define COUT_GTEST ANSI_TXT_GRN << GTEST_BOX //You could add the Default
#define COUT_GTEST_MGT COUT_GTEST << ANSI_TXT_MGT

所以我的代码是:

std::cout << COUT_GTEST_MGT << "random seed = " << random_seed << ANSI_TXT_DFT << std::endl;

I have just used std::cout with ansi color codes in linux but I believe the codes work in windows since win 10 anniversary update.

std::cout << "\033[0;32m" << "[          ] " << "\033[0;0m" 
<< "random seed = " << random_seed << std::endl;

or just create a header file and some #define statements and include it in my tests. I also like to format the text to stick out a little more too.

#define ANSI_TXT_GRN "\033[0;32m"
#define ANSI_TXT_MGT "\033[0;35m" //Magenta
#define ANSI_TXT_DFT "\033[0;0m" //Console default
#define GTEST_BOX "[     cout ] "
#define COUT_GTEST ANSI_TXT_GRN << GTEST_BOX //You could add the Default
#define COUT_GTEST_MGT COUT_GTEST << ANSI_TXT_MGT

So my code would be:

std::cout << COUT_GTEST_MGT << "random seed = " << random_seed << ANSI_TXT_DFT << std::endl;
独守阴晴ぅ圆缺 2024-12-19 11:13:59

以下作品,如果您保存在 .cmd 文件中,然后运行它

https://gist.githubusercontent.com/mlocati/fdabcaeb8071d5c75a2d51712db24011/raw/b710612d6320df7e146508094e84b92b34c77d48/win10colors.cmd

代码示例:

@echo off
cls
echo [101;93m STYLES [0m
echo ^<ESC^>[0m [0mReset[0m
echo ^<ESC^>[1m [1mBold[0m
echo ^<ESC^>[4m [4mUnderline[0m
echo ^<ESC^>[7m [7mInverse[0m
echo.

在此处输入图像描述

following works, if you save in a .cmd file and then run it

https://gist.githubusercontent.com/mlocati/fdabcaeb8071d5c75a2d51712db24011/raw/b710612d6320df7e146508094e84b92b34c77d48/win10colors.cmd

example of codes:

@echo off
cls
echo [101;93m STYLES [0m
echo ^<ESC^>[0m [0mReset[0m
echo ^<ESC^>[1m [1mBold[0m
echo ^<ESC^>[4m [4mUnderline[0m
echo ^<ESC^>[7m [7mInverse[0m
echo.

enter image description here

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