python3 unittest mock.patch 做用域

mock.patch 做用域

在使用 unittest.mock.patch 前必定要读一下文档,你百度出来的必定是最简单的例子也没有说明 patch 做用条件,只是访官网写的最简单例子,若是你刚接触 python unittest 可能会有很大一个坑在等着你python

unittest 官网文档的关于 patcher 的第一句话很重要:api

The patch decorators are used for patching objects only within the scope of the function they decorate. They automatically handle the unpatching for you, even if exceptions are raised. All of these functions can also be used in with statements or as class decorators.

stackoverflow 有个回答更容易理解一些:测试

patch works by (temporarily) changing the object that a name points to with another one. There can be many names pointing to any individual object, so for patching to work you must ensure that you patch the name used by the system under test.

The basic principle is that you patch where an object is looked up, which is not necessarily the same place as where it is defined.code

有时候认真看文档也是一种水平,我开始忽略了第一句话,白白浪费了不少时间。下面我用例子来讲明一下 patch 做用域:ip

假设我在 lib 模块下边有个类 Fooci

class Foo:

    def run(self):
        return 1

而后我在 api.bar 调用 Foo().run()作用域

# api.py 文件

from lib import Foo

def bar():
    instance = Foo()
    instance.run()

下边咱们来写 bar 的测试用例:文档

from unittest.mock import patch
from api import bar

@patch('lib.Foo')
def test_bar1(mock_foo):
    """"
    这是个反例,这里 mock 并无起到真正的做用
    """
    instance = mock_foo.return_value
    instance.run.return_value = 2
    print(bar().run()) # return 1
    assert bar().run() == 2 # False

@patch('api.Foo')
def test_bar2(mock_foo):
    instance = mock_foo.return_value
    instance.run.return_value = 2
    print(bar().run()) # return 2
    assert bar().run() == 2 # true
    assert bar() is instance

注意 test_bar1 mock 并无起到咱们所预期的做用, Foo().run() 依旧返回 1, 而 test_bar2 是咱们所预期的。get

test_bar1test_bar2 的区别在于 test_bar1 patch 的目标是 lib.Footest_bar2 patch 的目标是 api.Fooit

test_bar1test_bar2 就惟一的局别就是:@patch('lib.Foo') <==> @patch('api.Foo') ,能够看出 patch target 参数是在运行处对目标进行一个替换,而不是替换源类

相关文章
相关标签/搜索