本文介绍了我如何修补/模拟logging.getlogger()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有要测试的代码:
log = logging.getLogger(__name__)
class A(object):
def __init__(self):
log.debug('Init')
但是我不知道如何断言log.debug是用'Init'调用的
but I cannot figure out how to assert that log.debug was called with 'Init'
我尝试修补记录器,但检查它却只发现了一个getLogger模拟.
I tried patching logger but inspecting it I only found a getLogger mock.
我确定它很简单,但我只是想不通!
I'm sure its simple, but I just cant figure it!
在此先感谢您提供所有帮助!
Thanks in advance for any and all help!
推荐答案
替代解决方案,通过该解决方案,您还可以验证自己是否使用了正确的记录器:
Alternative solution, which lets you verify that you're using the correct logger too:
logger = logging.getLogger('path.to.module.under.test')
with mock.patch.object(logger, 'debug') as mock_debug:
run_code_under_test()
mock_debug.assert_called_once_with('Init')
这篇关于我如何修补/模拟logging.getlogger()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!