考虑以下代码:

import unittest
from unittest.mock import patch

class Foo(object):
    def __init__(self, bar=None):
        self.bar = bar

    def methodA(self):
        print("In methodA")

    def methodB(self):
        print("In methodB")


def my_func(bar):
    foo = Foo(bar)
    if foo.bar:
        foo.methodA()
    foo.methodB()


class MyTestCase(unittest.TestCase):
    def test_my_func(self):
        bar = None

        with patch("__main__.Foo", autospec=True) as foo:
            my_func(bar)
            foo.methodB.assert_called_once_with()


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


这个想法很简单。我有一个函数,该函数的行为会切换是否存在实例属性。我正在尝试编写一个单元测试,以根据属性验证仅执行某些Foo方法。

基于模拟库的patchautospeccing文档,我认为在autospec=True上下文管理器中设置patch()就足够了。没有。生成的Mock()正确包含methodAmethodB的模拟,但是测试失败,并显示以下错误:

======================================================================
ERROR: test_my_func (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "so.py", line 28, in test_my_func
    my_func(bar)
  File "trash.py", line 18, in my_func
    if foo.bar:
  File "/.../python3.3/unittest/mock.py", line 549, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'bar'


我确定我缺少明显的东西,但似乎无法弄清楚。如何对my_func()进行单元测试?

最佳答案

我的问题的一部分是对补丁行为的误解。我设置的上下文管理器将返回__main__.Foo模拟的实例,而不是my_func()中使用的相同实例。换句话说,即使我能够在没有autospec的情况下正确模拟Foo,也无法通过其任何方法执行assert_called_once_with():这不是同一对象。

一种解决方案是模拟方法本身。这有效:

def test_my_func(self):
    bar = None
    with patch('__main__.Foo.methodB') as mock_methodB:
        my_func(bar)
        mock_methodB.assert_called_once_with()


另一种方法是修改my_func()以返回foo:

def my_func(bar):
    foo = Foo(bar)
    if foo.bar:
        foo.methodA()
    foo.methodB()
    return foo


因为该函数返回了测试中的模拟,所以以下内容应该起作用:

def test_my_func(self):
    bar = None
    with patch('__main__.Foo', spec=True, bar=None):
        foo = my_func(bar)
        assert foo.methodB.called
        assert not foo.methodA.called

09-25 22:14
查看更多