避免执行模拟类的 __init__

Jes*_*ebb 5 python mocking python-2.7 python-mock python-unittest

我有一个具有昂贵__init__功能的类。我不希望从测试中调用这个函数。

出于本示例的目的,我创建了一个在__init__以下位置引发异常的类:

class ClassWithComplexInit(object):

    def __init__(self):
        raise Exception("COMPLEX!")

    def get_value(self):
        return 'My value'
Run Code Online (Sandbox Code Playgroud)

我有第二个类,它构造一个实例ClassWithComplexInit并使用它的函数。

class SystemUnderTest(object):

    def my_func(self):
        foo = ClassWithComplexInit()
        return foo.get_value()
Run Code Online (Sandbox Code Playgroud)

我正在尝试围绕SystemUnderTest#my_func(). 我遇到的问题是无论我如何尝试模拟ClassWithComplexInit,该__init__函数总是被执行并引发异常。

class TestCaseWithoutSetUp(unittest.TestCase):

    @mock.patch('mypackage.ClassWithComplexInit.get_value', return_value='test value')
    def test_with_patched_function(self, mockFunction):
        sut = SystemUnderTest()
        result = sut.my_func()  # fails, executes ClassWithComplexInit.__init__()
        self.assertEqual('test value', result)

    @mock.patch('mypackage.ClassWithComplexInit')
    def test_with_patched_class(self, mockClass):
        mockClass.get_value.return_value = 'test value'
        sut = SystemUnderTest()
        result = sut.my_func()  # seems to not execute ClassWithComplexInit.__init__()
        self.assertEqual('test value', result)  # still fails
        # AssertionError: 'test value' != <MagicMock name='ClassWithComplexInit().get_value()' id='4436402576'>
Run Code Online (Sandbox Code Playgroud)

上面的第二种方法是我从这个类似的问答中得到的,但它也不起作用。它似乎没有运行该__init__函数,但我的断言失败了,因为结果最终是一个模拟实例,而不是我的值。

我还尝试patchsetUp函数中配置一个实例,使用startstop函数作为文档建议

class TestCaseWithSetUp(unittest.TestCase):

    def setUp(self):
        self.mockClass = mock.MagicMock()
        self.mockClass.get_value.return_value = 'test value'
        patcher = mock.patch('mypackage.ClassWithComplexInit', self.mockClass)
        patcher.start()
        self.addCleanup(patcher.stop)

    def test_my_func(self):
        sut = SystemUnderTest()
        result = sut.my_func()  # seems to not execute ClassWithComplexInit.__init__()
        self.assertEqual('test value', result)  # still fails
        # AssertionError: 'test value' != <MagicMock name='mock().get_value()' id='4554658128'>
Run Code Online (Sandbox Code Playgroud)

这似乎也避免了我的__init__功能,但我设置的值get_value.return_value没有得到尊重,get_value()并且仍在返回一个MagicMock实例。

如何模拟一个__init__由我的被测代码实例化的复杂类?理想情况下,我想要一个适用于 TestCase 类中的许多单元测试的解决方案(例如不需要patch每个测试)。

我正在使用 Python 版本2.7.6

che*_*ner 4

首先,您需要使用与修补创建的名称相同的名称foo,即

class SystemUnderTest(object):

    def my_func(self):
        foo = mypackage.ClassWithComplexInit()
        return foo.get_value()
Run Code Online (Sandbox Code Playgroud)

其次,您需要配置正确的模拟对象。您正在配置ClassWithComplexInit.get_value,即未绑定的方法,但您需要配置ClassWithComplexInit.return_value.get_value,它是Mock将实际调用的对象foo.get_value()

@mock.patch('mypackage.ClassWithComplexInit')
def test_with_patched_class(self, mockClass):
    mockClass.return_value.get_value.return_value = 'test value'
    sut = SystemUnderTest()
    result = sut.my_func()  # seems to not execute ClassWithComplexInit.__init__()
    self.assertEqual('test value', result)  # still fails
Run Code Online (Sandbox Code Playgroud)