如何运行gtest没有从谷歌日志记录

How to run gtests without logging from google log?

本文关键字:谷歌 日志 记录 gtest 何运行 运行      更新时间:2023-10-16

我正在用gtest运行单元测试。然而,我也使用谷歌博客在我正在测试的代码。不幸的是,这个输出妨碍了测试结果,看起来很混乱。我怎样才能去掉log输出呢?

这似乎有效,抑制所有日志消息。

int main(int argc, char * argv[]) {
  FLAGS_logtostderr = true;
  FLAGS_minloglevel = 5;
  google::InitGoogleLogging(argv[0]);
  // Start running unit tests here
}

从高级指南中,定义自己的空EventListener并超越所有调试日志,然后从事件侦听器中删除默认打印机。

    int main(int argc, char** argv) {
        ::testing::InitGoogleTest(&argc, argv);
        // Gets hold of the event listener list.
        ::testing::TestEventListeners& listeners =
        ::testing::UnitTest::GetInstance()->listeners();
        // Removes the default console output listener from the list so it will
        // not receive events from Google Test and won't print any output. Since
        // this operation transfers ownership of the listener to the caller we
        // have to delete it as well.
        delete listeners.Release(listeners.default_result_printer());
       // Adds a listener to the end.  Google Test takes the ownership.
       // Basically you can define an empty class MinimalistPrinter
       // derived from EmptyTestEventListener
       listeners.Append(new MinimalistPrinter);
       return RUN_ALL_TESTS();
  }

示例程序在这里