如何在不从谷歌日志记录的情况下运行 gtests?

发布于 2024-12-03 11:57:00 字数 94 浏览 1 评论 0原文

我正在使用 gtest 运行单元测试。不过,我也在我正在测试的代码中使用 google glog。不幸的是,这个输出妨碍了测试结果并且看起来很混乱。如何摆脱 glog 输出?

I'm running unit tests with gtest. However I'm also using google glog in the code I'm testing. Unfortunately this output gets in the way of the test results and looks messy. How do I do I get rid of the glog output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不是你的备胎 2024-12-10 11:57:00

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

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

This seems to work, suppressing all log messages.

int main(int argc, char * argv[]) {
  FLAGS_logtostderr = true;
  FLAGS_minloglevel = 5;
  google::InitGoogleLogging(argv[0]);
  // Start running unit tests here
}
白鸥掠海 2024-12-10 11:57:00

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

    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();
  }

示例程序此处

From the Advanced Guide, define your own empty EventListener and surpass all debug log and then remove the default printer from the event listener.

    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();
  }

Sample program here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文