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。



