单元测试期间的Python日志捕获 [英] Python Log Capture During Unit Tests

查看:113
本文介绍了单元测试期间的Python日志捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获在python单元测试过程中创建的日志,并在此处看到代码: https://stackoverflow .com/a/1049375/576333 .

I am trying to capture logs that are created during the course of my python unittests and saw the code here: https://stackoverflow.com/a/1049375/576333.

但是,我无法使该代码正常工作,并且会自由地承认我对python的日志记录模块的理解并不理想.

However, I cannot get this code to work and will freely admit that my understanding of python's logging module is not fantastic.

本质上,我在该帖子中添加了模拟处理程序,如下所示: my_logger.addHandler(MockLoggingHandler)

Essentially, I am adding the mock handler in that post like so: my_logger.addHandler(MockLoggingHandler)

,当代码调用说my_logger.info(some_message)时,我得到 AttributeError:类MockLoggingHandler没有属性级别"

and when the code calls, say, my_logger.info(some_message) I get AttributeError: class MockLoggingHandler has no attribute 'level'

我不确定从哪里开始找出MockLoggingHandler类中缺少的内容.顺便说一句,我正在使用Python 2.6.8.

I am not sure where to start on figuring out what is missing from the MockLoggingHandler class. BTW, I am using Python 2.6.8.

推荐答案

您需要添加该类的实例,如下所示:

You need to add an instance of the class, like so:

my_logger.addHandler(MockLoggingHandler())

注意MockLoggingHandler之后的().

这篇关于单元测试期间的Python日志捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆