python-mock: 'self'参数缺少默认值

标签 python python-unittest python-mock

这曾经适用于 python 模拟版本 1.0.1,但在我升级到模拟版本 1.3.0 后开始失败。我在 Mac OS X Yosemite 10.10.5 上运行 python 版本 2.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')\nActual call: doStuff(action='swim')\n'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 ===========================================================================

最佳答案

Outer.MyClass.doStuff 的实例方法模拟上省略 spec_set (和 spec)。 self 不会被 assert_used_once_with 捕获/断言,并且您的测试将会通过。

    @patch.object(Outer, "MyClass", autospec=True,
          return_value=Mock(spec_set=Outer.MyClass, 
                            doStuff=Mock()))

关于python-mock: 'self'参数缺少默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34376826/

相关文章:

python - 如何对 assertDictNotEqual 进行单元测试?

python - django 使用装置进行单元测试 - 对象匹配查询不存在

python - 使用 s3fs(由 dask)测试模拟 S3 存储桶的行为是意外的

python - 如何在测试运行时类型检查期间模拟 Python 类?

python - 断言对不同模拟对象的调用序列

Python模拟elasticsearch连接生成器 `with`语句

python - 我正在寻找一种在 python 中生成代码的简单方法

python - 季节分解

python - 在 OpenCV (Python) 中,为什么我从灰度图像中获得 3 channel 图像?

python - 使用Python自动裁剪图像