bash 如何将 Google 测试输出打印到文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19736536/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:28:58  来源:igfitidea点击:

How to print Google Test output to a text file?

c++cbashcmakegoogletest

提问by RobotEyes

I have gtest up and running using code as shown below. I would like to print the test output to a text file as opposed to displaying it in the console. Is there a way of doing this?

我使用如下所示的代码启动并运行了 gtest。我想将测试输出打印到文本文件,而不是在控制台中显示它。有没有办法做到这一点?

I run the tests using cmakefrom the console: cmake CMakeLists.txt && make && ./runTests.

cmake从控制台使用运行测试:cmake CMakeLists.txt && make && ./runTests.

#include "cw-test.c"
#include <stdio.h>
#include <gtest/gtest.h>

TEST(InputValidationTest, ValidateEntryLine)
{
    ...
}

...

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

回答by crayzeewulf

You can redirect the output of your runTestscommand to a file:

您可以将runTests命令的输出重定向到文件:

cmake CMakeLists.txt && make && ./runTests > test_output.txt

Also, see thiswhich explains why you do not need the &I had used in my comment. As Awaken's answer says, the &redirects both the stdoutand stderrto the same file. But since googletestoutput always goes to stdoutyou may leave out the &.

另外,请参阅解释了为什么您不需要&我在评论中使用的。正如 Awaken 的回答所说,&stdout和重定向stderr到同一个文件。但由于googletest输出总是到stdout你可能会遗漏&.

回答by Awaken

crayzeewulf's comment will work for any Unix program. What "&>" means is to redirect output in "stdout" and "stderr" to some other location you specify.

crayzeewulf 的评论适用于任何 Unix 程序。“&>”的意思是将“stdout”和“stderr”中的输出重定向到您指定的其他位置。

More information can be found here. http://www.mathinfo.u-picardie.fr/asch/f/MeCS/courseware/users/help/general/unix/redirection.html

更多信息可以在这里找到。 http://www.mathinfo.u-picardie.fr/asch/f/MeCS/courseware/users/help/general/unix/redirection.html