简体   繁体   English

在 Google Test 中打印附加输出

[英]Printing additional output in Google Test

I'm using the googletest C++ testing framework .我正在使用googletest C++ 测试框架 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.我在 googletest 文档中找到了Logging Additional Information ,但这似乎只是将结构化数据发送到 XML 输出,而不是标准控制台输出。

Is there a googletest function I can call inside my unit test that outputs text in this format?我可以在单元测试中调用以这种格式输出文本的 googletest 函数吗? 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.手动将数据发送到cout有效,但它不包括通常的彩色输出,因此我必须通过打印 13 个空格或其他任何内容来显式缩进输出。

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

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

You could write a wrapper for the default printer PrettyUnitTestResultPrinter to also print out test properties.您可以为默认打印机PrettyUnitTestResultPrinter编写一个包装器来打印测试属性。 You can get the default printer with listeners.default_result_printer() (see below).您可以使用listeners.default_result_printer()获取默认打印机(见下文)。 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:您必须实现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();
}

Then you can replace the default listener for your tests like it's done in the google test sample :然后,您可以像在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();

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.我刚刚在linux 中使用std::cout和 ansi 颜色代码,但我相信这些代码自赢得 10 周年更新以来在 Windows 中有效。

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.或者只是创建一个头文件和一些#define语句并将其包含在我的测试中。 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;

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如果需要,您可以在http://code.google.com/p/googletest/issues/list 上记录增强任务

Take care.小心。

following works, if you save in a .cmd file and then run it以下工作,如果您保存在 .cmd 文件中然后运行它

https://gist.githubusercontent.com/mlocati/fdabcaeb8071d5c75a2d51712db24011/raw/b710612d6320df7e146508094e84b92b34c77d48/win10colors.cmd 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.

在此处输入图片说明

Printing additional output in gTest also possible like this:也可以像这样在 gTest 中打印附加输出:

  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. std::cout , std::cerr在 gTest 中不起作用。

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

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