python unittest mock无法处理部分限定名称

时间:2016-03-01 01:35:14

标签: python unit-testing mocking python-unittest

如果我有两个包含以下内容的文件:

test_helper.py

class Installer:
    def __init__(self, foo, bar, version):
        # Init stuff
        raise Exception('If we're here, mock didn't work')

    def __enter__(self):
        return self

    def __exit__(self, type, value, tb):
        # cleanup
        pass

    def install(self):
        # Install stuff
        raise Exception('If we're here, mock didn't work')

test.py

import unittest
from mock import patch
from test_helper import Installer

class Deployer:
    def deploy(self):
        with Installer('foo', 'bar', 1) as installer:
            installer.install()

class DeployerTest(unittest.TestCase):
    @patch('tests.test_helper.Installer', autospec=True)
    def testInstaller(self, mock_installer):
        deployer = Deployer()
        deployer.deploy()
        mock_installer.assert_called_once_with('foo', 'bar', 1)

上面的代码无法正确测试。模拟未正确应用:

  File "/Library/Python/2.7/site-packages/mock-1.3.0-py2.7.egg/mock/mock.py", line 947, in assert_called_once_with
    raise AssertionError(msg)
AssertionError: Expected 'Installer' to be called once. Called 0 times.

如果我在test.py中进行了以下更改:

  1. from test_helper import Installer更改为import test_helper
  2. with Installer('foo', 'bar', 1) as installer:更改为with test_helper.Installer('foo', 'bar', 1) as installer:
  3. 然后代码可以运行。为什么模拟仅在我使用完全限定名称时适用?是否应该在部分合格的情况下工作?

1 个答案:

答案 0 :(得分:0)

您正在测试Deployer内的test.py课程,该课程正在呼叫Installer。这个安装程序是你想要模拟的。所以,你的装饰者应该与此相关。

我完全不知道你在哪里测试。但是作为一个例子,如果你从与test.py相同的级别运行测试,那么你可以简单地对装饰器执行此操作,它应该可以工作:

import unittest

from dower import Installer
from mock import patch

class Deployer:
    def deploy(self):
        with Installer('foo', 'bar', 1) as installer:
            installer.install()

class DeployerTest(unittest.TestCase):
    @patch('test.Installer', autospec=True)
    def testInstaller(self, mock_installer):
        deployer = Deployer()
        deployer.deploy()
        mock_installer.assert_called_once_with('foo', 'bar', 1)


if __name__ == '__main__':
    unittest.main()

注意:您不应该在Installer模块中进行模拟。在这种情况下,您不会关心关于安装程序。只需返回Mock,即可继续测试Deployer的行为。可以这样想,你会发现为什么你必须模拟你正在测试的东西。