Python unittest模拟:是否可以在测试时模拟方法的默认参数的值?

发布于 2021-01-29 14:58:46

我有一个接受默认参数的方法:

def build_url(endpoint, host=settings.DEFAULT_HOST):
    return '{}{}'.format(host, endpoint)

我有一个使用此方法的测试用例:

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在导入时设置了关键字参数,并且未考虑我的模拟。

调试器

(Pdb) settings
<MagicMock name='settings' id='85761744'>                                                                                                                                                                                               
(Pdb) settings.DEFAULT_HOST
'domain'
(Pdb) host
'host-from-settings.com'

有没有一种方法可以在测试时覆盖此值,以便我可以使用模拟settings对象行使默认路径?

关注者
0
被浏览
74
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    func_defaults定义函数后,函数会将其参数默认值存储在属性中,因此可以对其进行修补。就像是

    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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看