c++ - 如何在单元测试中测试 boost 记录器输出?

标签 c++ logging boost googletest boost-log

我的应用程序使用 BOOST_LOG_TRIVIAL 来记录消息。我认为检查测试 (gtest) 在给定场景中是否写入了正确的消息是很好的。

有没有办法以某种方式访问​​代码被调用后写入的内容?还是首先必须以某种方式对其进行模拟?

我为此在谷歌上搜索了很多,但没有找到任何说明,所以要么我问错了问题,要么没有人认为应该这样做。

最佳答案

在您的 googletest 套件中,您可以使用 boost::log 设施 每个测试用例将 BOOST_LOG_TRIVIAL 消息重定向到一个文件。 在写完你想要的 BOOST_LOG_TRIVIAL 消息之后,你就可以 刷新文件,打开它,并检查它是否包含您期望的内容。 例如:

gtester.cpp

#include <gtest/gtest.h>
#include <boost/shared_ptr.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/trivial.hpp>
#include <string>
#include <fstream>

using sink_t = boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>;

struct boost_log_tester : ::testing::Test {
    void SetUp() {
        file_sink = boost::log::add_file_log("boost.log");
    }
    void TearDown() {
        boost::log::core::get()->remove_sink(file_sink);
        file_sink.reset();
    }
protected:
    boost::shared_ptr<sink_t> file_sink;
};

TEST_F(boost_log_tester,info_msg)
{
    std::string msg = "An informational severity message";
    BOOST_LOG_TRIVIAL(info) << msg;
    file_sink->flush();
    std::ifstream captured_cout("boost.log");
    ASSERT_TRUE(captured_cout.good()) << "Failure executing test: Could not open `boost.log` for reading";
    std::string cout_str;
    std::getline(captured_cout,cout_str);
    EXPECT_NE(cout_str.find(msg),std::string::npos);
}

TEST_F(boost_log_tester,error_msg)
{
    std::string msg = "An error severity message";
    BOOST_LOG_TRIVIAL(error) << msg;
    file_sink->flush();
    std::ifstream captured_cerr("boost.log");
    ASSERT_TRUE(captured_cerr.good()) << "Failure executing test: Could not open `boost.log` for reading";
    std::string cerr_str;
    std::getline(captured_cerr,cerr_str);
    EXPECT_NE(cerr_str.find(msg),std::string::npos);
}

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

编译链接:

$ g++ -Wall -Wextra -DBOOST_LOG_DYN_LINK -c gtester.cpp
$ g++ -o gtester gtester.o -lboost_log -lboost_thread -lboost_system -lgtest -pthread

它的运行方式如下:

$ ./gtester 
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from boost_log_tester
[ RUN      ] boost_log_tester.info_msg
[       OK ] boost_log_tester.info_msg (0 ms)
[ RUN      ] boost_log_tester.error_msg
[       OK ] boost_log_tester.error_msg (2 ms)
[----------] 2 tests from boost_log_tester (2 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (2 ms total)
[  PASSED  ] 2 tests.

关于c++ - 如何在单元测试中测试 boost 记录器输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53372078/

相关文章:

java - Rest API 日志记录实践问题

c++ - 错误 C2011 : 'member' : 'struct' type redefinition on moving a struct to a new header file

c++ - boost 序列化字符串 *

重定向到文件时 Python 日志出现较晚

c++ - "thrift -gen cpp"生成的代码无法在 macOS : 'boost/tr1/functional.hpp' file not found 上编译

c++ - 为什么不能将 const set<Derived*> 作为 const set<Base*> 传递给函数?

C++ 类 vector 推回错误

c++ - 为共享指针 move 构造函数和 = 运算符

c++ - 具有相同名称的非托管 C++ DLL 在同一进程中共存

objective-c - 有没有办法在任何 NSLog 语句的开头设置断点?