如何将谷歌测试输出打印到文本文件

How to print Google Test output to a text file?

本文关键字:打印 文本 文件 输出 测试 谷歌      更新时间:2023-10-16

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

我使用控制台中的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();
}
您可以将

runTests命令的输出重定向到文件:

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

另外,请参阅此内容,其中解释了为什么您不需要我在评论中使用的&。正如 Awaken 的回答所说,&stdoutstderr重定向到同一个文件。但是由于googletest输出总是转到stdout因此您可以省略&

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

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