C++ 在 Google 测试中打印额外的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7923903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing additional output in Google Test
提问by Greg Hewgill
I'm using the googletest C++ testing framework. Normally the textual output of running a test looks like this:
我正在使用googletest C++ 测试框架。通常,运行测试的文本输出如下所示:
[ 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 Informationin 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? 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.
是否有一个 googletest 函数可以在我的单元测试中调用以这种格式输出文本?手动将数据发送到cout
作品,但它不包括通常的彩色输出,因此我必须通过打印 13 个空格或其他任何内容来显式缩进输出。
回答by Martin Nowak
Simply printing to stderr will work in the default test configuration.
只需打印到 stderr 即可在默认测试配置中工作。
std::cerr << "[ ] random seed = " << random_seed << std::endl;
回答by kossmoboleat
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:
您可以为默认打印机编写一个包装器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();
}
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();
回答by Lee
I have just used std::cout
with ansi color codes in linuxbut I believe the codes work in windows since win 10 anniversary update.
我刚刚std::cout
在linux 中使用了 ansi 颜色代码,但我相信自 win 10 周年更新以来,这些代码可以在 windows 中使用。
std::cout << "3[0;32m" << "[ ] " << "3[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
语句并将其包含在我的测试中。我也喜欢格式化文本以使其更加突出。
#define ANSI_TXT_GRN "3[0;32m"
#define ANSI_TXT_MGT "3[0;35m" //Magenta
#define ANSI_TXT_DFT "3[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;
回答by Captain Charmi
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.
小心。