蟒蛇 +鼻子:对记录的文本做出断言?
是否有一些简单的方法可以使用 nose
捕获记录的消息并对其进行断言?
例如,我希望能够执行以下操作:
cook_eggs()
assert_logged("eggs are ready!")
Is there some simple way of capturing and making assertions about logged messages with nose
?
For example, I'd like to be able to do something like:
cook_eggs()
assert_logged("eggs are ready!")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建一个自定义处理程序,它可以检查通过日志记录发送的消息。 BufferingHandler 非常适合这项工作。
您可能还想在测试中将处理程序附加到您在代码中使用的任何记录器,例如
logging.getLogger('foo').addHandler(...)
。您最终可以将处理程序附加到测试用例的setUp
和tearDown
方法中。You can create a custom handler which can check for the message being sent through logging. The BufferingHandler is a perfect match for this job.
You might also want to attach in your test the handler to any logger you are using in your code, such as
logging.getLogger('foo').addHandler(...)
. You could eventually attach the handler in thesetUp
andtearDown
methods of your test case.这就是“模拟对象”的用途。
您可以使用日志记录的模拟版本,它将正确缓冲日志消息,以便您稍后可以对它们进行断言。
This is what "Mock Objects" are for.
You can use a mock version of logging which will properly buffer the log messages so that you can later make assertions about them.
FWIW,在 datalad 项目中,我们需要类似的功能,但也只是吞下日志(并可能进行内省)。所以这里出现了解决方案——swallow_logs上下文处理程序: https://github.com/datalad/datalad/blob/master/datalad/utils.py#L296(当前位于 b633c9da46ab9cccde3d4767928d167a91857153)。所以现在在测试中我们做类似的事情
Just FWIW, in datalad project we needed similar functionality but also to just swallow the logs (and possibly introspect). So here came the solution -- swallow_logs context handler: https://github.com/datalad/datalad/blob/master/datalad/utils.py#L296 (currently at b633c9da46ab9cccde3d4767928d167a91857153). So now in the test we do smth like