本文介绍了缺少缺省值的python-mock:';self';参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这曾经适用于python mock版本1.0.1,但在我升级到mock版本1.3.0后开始失败。我在Mac OS X Yosemite 10.10.5上运行的是Python2.7.10版。
我将逻辑从现有的生产测试减少到以下复制该问题的虚拟测试:
import unittest
import mock
from mock import Mock, patch
class Outer(object):
class MyClass(object):
def doStuff(self, action):
pass
@patch.object(Outer, "MyClass", autospec=True,
return_value=Mock(spec_set=Outer.MyClass,
doStuff=Mock(spec_set=Outer.MyClass.doStuff)))
class MyTestCase(unittest.TestCase):
def testDoStuff(self, myClassMock):
obj = myClassMock()
obj.doStuff(action="swim")
obj.doStuff.assert_called_once_with(action="swim")
失败输出如下所示:
$ py.test -v new_mock_test.py
====================================================================================== test session starts =======================================================================================
platform darwin -- Python 2.7.10, pytest-2.8.5, py-1.4.30, pluggy-0.3.1 -- /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
cachedir: .cache
rootdir: /Users/me/TempOnDesktop, inifile:
plugins: cov-1.6, xdist-1.8
collected 1 items
new_mock_test.py::MyTestCase::testDoStuff FAILED
============================================================================================ FAILURES ============================================================================================
_____________________________________________________________________________________ MyTestCase.testDoStuff _____________________________________________________________________________________
self = <new_mock_test.MyTestCase testMethod=testDoStuff>, myClassMock = <MagicMock name='MyClass' spec='MyClass' id='4367040848'>
def testDoStuff(self, myClassMock):
obj = myClassMock()
obj.doStuff(action="swim")
> obj.doStuff.assert_called_once_with(action="swim")
new_mock_test.py:26:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../Library/Python/2.7/lib/python/site-packages/mock/mock.py:948: in assert_called_once_with
return self.assert_called_with(*args, **kwargs)
../Library/Python/2.7/lib/python/site-packages/mock/mock.py:937: in assert_called_with
six.raise_from(AssertionError(_error_message(cause)), cause)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
value = AssertionError("Expected call: doStuff(action='swim')
Actual call: doStuff(action='swim')
'self' parameter lacking default value",)
from_value = TypeError("'self' parameter lacking default value",)
def raise_from(value, from_value):
> raise value
E AssertionError: Expected call: doStuff(action='swim')
E Actual call: doStuff(action='swim')
E 'self' parameter lacking default value
../Library/Python/2.7/lib/python/site-packages/six.py:718: AssertionError
========================================================================== 1 failed, 2 pytest-warnings in 0.13 seconds ===========================================================================
推荐答案
spec_set
(和spec
)对Outer.MyClass.doStuff
的实例方法模拟。assert_called_once_with
将不会捕获/断言self
,您的测试将通过。
@patch.object(Outer, "MyClass", autospec=True,
return_value=Mock(spec_set=Outer.MyClass,
doStuff=Mock()))
这篇关于缺少缺省值的python-mock:';self';参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!