我正在使用Python testfixtures.LogCapture库来测试日志输出。这适用于检查方法调用的完整日志输出:
with LogCapture() as captured_log:
foo.do_something()
captured_log.check(
('root', 'INFO', 'first log message'),
('root', 'INFO', 'second log message'),
('root', 'INFO', 'third log message'),
)
有没有办法检查单个日志条目是否存在,而不检查所有周围的行?
有些事情:
# Pseudocode
captured_log.has_entry(
('root', 'INFO', 'second log message')
)
答案 0 :(得分:1)
您可以使用LogCapture.actual()
获取一个生成器,它将为您提供所有日志条目。然后,您可以查看是否存在预期的日志条目。
它看起来像:
with LogCapture() as captured_log:
foo.do_something()
expected_log = (
('root', 'INFO', 'second log message')
)
assert expected_log in LogCapture.actual()
API doc:https://testfixtures.readthedocs.io/en/latest/api.html#testfixtures.LogCapture.actual