【问题标题】:How to print Google Test output to a text file?如何将 Google 测试输出打印到文本文件?
【发布时间】:2013-11-01 22:40:05
【问题描述】:

我已经使用如下所示的代码启动并运行了 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();
}

【问题讨论】:

  • 这对你有用吗:cmake CMakeLists.txt &amp;&amp; make &amp;&amp; ./runTests &amp;&gt; test_output.txt(见this)?
  • @crayzeewulf 是的!很好,谢谢。把它放在答案中并获得一些分数。这适用于任何程序还是与 gtest 相关?
  • RobotEyes,完成。此外,这种类型的 redirection 将适用于您在 shell 中运行的任何命令。
  • bash 和 cmake 独立:gtest_output option。 Windows,VS:选项 --gtest_output=xml--gtest_output=xml:./ 与当前文件夹中的结果一起使用(我认为是解决方案之一)。

标签: c++ c bash cmake googletest


【解决方案1】:

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

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

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

【讨论】:

  • 但是被测代码本身可能正在记录到标准错误。如果您仅使用 >.,这些打印输出将不会出现在输出文件中
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 2011-04-03
    相关资源
    最近更新 更多