在 Google 测试中打印附加输出
我使用的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需打印到 stderr 即可在默认测试配置中工作。
Simply printing to stderr will work in the default test configuration.
在 gTest 中打印附加输出也可能是这样的:
std::cout、std::cerr 在 gTest 中不起作用。
Printing additional output in gTest also possible like this:
std::cout, std::cerr will not work within gTest.
您可以为默认打印机
PrettyUnitTestResultPrinter
编写一个包装器来打印测试属性。您可以使用listeners.default_result_printer()
获取默认打印机(见下文)。您必须实现EmptyTestEventListener
并更改方法PrettyUnitTestResultPrinter::OnTestEnd()
(在 gtest.cc 中)并使用结果属性:test_info.result()->GetTestProperty(i)
就像 XML 输出的打印机:然后您可以替换测试的默认侦听器,就像在 Google 测试示例:
You could write a wrapper for the default printer
PrettyUnitTestResultPrinter
to also print out test properties. You can get the default printer withlisteners.default_result_printer()
(see below). You would have to implementEmptyTestEventListener
and change the methodPrettyUnitTestResultPrinter::OnTestEnd()
(in gtest.cc) and use the results properties:test_info.result()->GetTestProperty(i)
like the printer for XML output:Then you can replace the default listener for your tests like it's done in the google test sample:
不,搜索了标题,没有在中间添加您自己的日志。可能有什么要求。如果需要,您可以在 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.
我刚刚在 linux 中使用了
std::cout
和 ansi 颜色代码,但我相信自从 win 10 周年更新以来这些代码可以在 Windows 中工作。或者只是创建一个头文件和一些
#define
语句并将其包含在我的测试中。我还喜欢设置文本格式以使其更加突出。所以我的代码是:
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.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.So my code would be:
以下作品,如果您保存在 .cmd 文件中,然后运行它
https://gist.githubusercontent.com/mlocati/fdabcaeb8071d5c75a2d51712db24011/raw/b710612d6320df7e146508094e84b92b34c77d48/win10colors.cmd
代码示例:
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: