Python unittest模拟:是否可以在测试时模拟方法的默认参数的值? [英] Python unittest mock: Is it possible to mock the value of a method's default arguments at test time?
问题描述
我有一个接受默认参数的方法:
I have a method that accepts default arguments:
def build_url(endpoint, host=settings.DEFAULT_HOST):
return '{}{}'.format(host, endpoint)
我有一个使用此方法的测试用例:
I have a test case that exercises this method:
class BuildUrlTestCase(TestCase):
def test_build_url(self):
""" If host and endpoint are supplied result should be 'host/endpoint' """
result = build_url('/end', 'host')
expected = 'host/end'
self.assertEqual(result,expected)
@patch('myapp.settings')
def test_build_url_with_default(self, mock_settings):
""" If only endpoint is supplied should default to settings"""
mock_settings.DEFAULT_HOST = 'domain'
result = build_url('/end')
expected = 'domain/end'
self.assertEqual(result,expected)
如果我将调试点放在build_url
中并检查此属性,则settings.DEFAULT_HOST
返回模拟值.但是,测试继续失败,并且断言指示host
被分配了我实际的settings.py
中的值.我知道这是因为host
关键字参数是在导入时设置的,并且未考虑我的模拟.
If I drop a debug point in build_url
and inspect this attribute settings.DEFAULT_HOST
returns the mocked value. However the test continues to fail and the assertion indicates host
is assigned the value from my actual settings.py
. I know this is because the host
keyword argument is set at import time and my mock is not considered.
调试器
(Pdb) settings
<MagicMock name='settings' id='85761744'>
(Pdb) settings.DEFAULT_HOST
'domain'
(Pdb) host
'host-from-settings.com'
是否可以在测试时覆盖此值,以便可以使用模拟的settings
对象行使默认路径?
Is there a way to override this value at test time so that I can exercise the default path with a mocked settings
object?
推荐答案
定义函数后,函数会将其参数默认值存储在func_defaults
属性中,因此您可以对其进行修补.像
Functions store their parameter default values in the func_defaults
attribute when the function is defined, so you can patch that. Something like
def test_build_url(self):
""" If only endpoint is supplied should default to settings"""
# Use `func_defaults` in Python2.x and `__defaults__` in Python3.x.
with patch.object(build_url, 'func_defaults', ('domain',)):
result = build_url('/end')
expected = 'domain/end'
self.assertEqual(result,expected)
我使用patch.object
作为上下文管理器而不是装饰器,以避免不必要的补丁对象作为参数传递给test_build_url
.
I use patch.object
as a context manager rather than a decorator to avoid the unnecessary patch object being passed as an argument to test_build_url
.
这篇关于Python unittest模拟:是否可以在测试时模拟方法的默认参数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!