Python:无论从哪里导入模块,都可以模拟补丁

时间:2016-05-18 10:44:55

标签: python unit-testing python-import monkeypatching

我需要确保运行单元测试不会触发调用一个沉重的外部世界函数,比如说,这个:

# bigbad.py
def request(param):
    return 'I searched the whole Internet for "{}"'.format(param)

多个模块使用此功能(bigbad.request)并以不同方式导入它(在现实生活中它也可以从外部库导入)。比方说,有两个模块,a和b,其中b取决于a,两者都使用函数:

# a.py, from...import
from bigbad import request

def routine_a():
    return request('a')

# b.py, imports directly
import a
import bigbad

def routine_b():
    resp_a = a.routine_a()
    return 'resp_a: {}, resp_b=request(resp_a): {}'.format(resp_a, bigbad.request(resp_a))

有没有办法确保没有调用bigbad.request?此代码仅模拟其中一个导入:

# test_b.py
import unittest
from unittest import mock
import b

with mock.patch('bigbad.request') as mock_request:
    mock_request.return_value = 'mocked'
    print(b.routine_b())

显然我可以重构b并改变进口,但这样我无法保证未来发展中的人不会破坏这一规定。我认为测试应该测试行为而不是实现细节。

3 个答案:

答案 0 :(得分:1)

# a.py, from...import
from bigbad import request

为确保永远不会调用原始request,您必须修补导入引用的所有位置:

import mock
with mock.patch('a.request', return_value='mocked') as mock_request:
    ...

这很乏味,因此如果可能,请不要在代码中执行from bigbad import request,而是使用import bigbad; bigbad.request

另一种解决方案:如果可能,请更改bigbad.py

# bigbad.py
def _request(param):
    return 'I searched the whole Internet for "{}"'.format(param)


def request(param):
    return _request(param)

然后,即使某些代码from bigbad import request,您也可以with mock.patch('bigbad._request', return_value='mocked') as mock_request:

答案 1 :(得分:0)

import bigbad
bigbad.request = # some dummy function

只要在运行/导入from bigbad import request的任何模块运行之前,它就会运行。也就是说,只要它们运行,它们就会收到虚拟函数。

答案 2 :(得分:0)

对于将来遇到这个问题的任何人,我编写了一个函数来修补给定符号的所有导入。

此函数为给定符号(整个模块、特定函数或任何其他对象)的每次导入返回修补程序列表。然后可以在测试装置的设置/拆卸区域启动/停止这些修补程序(请参阅文档字符串以获取示例)。

工作原理:

  • 遍历 sys.modules 中每个当前可见的模块
  • 如果模块名称以match_prefix(可选)开头且不包含skip_substring(可选),则遍历模块中的每个局部
  • 如果本地是 target_symbol,则为它创建一个修补程序,位于导入它的模块的本地

我建议使用 skip_substring='test' 之类的参数,这样您就不会修补测试套件导入的内容。

from typing import Any, Optional
import unittest.mock as mock
import sys

def patch_all_symbol_imports(
        target_symbol: Any, match_prefix: Optional[str] = None,
        skip_substring: Optional[str] = None
):
    """
    Iterate through every visible module (in sys.modules) that starts with
    `match_prefix` to find imports of `target_symbol` and return a list
    of patchers for each import.

    This is helpful when you want to patch a module, function, or object
    everywhere in your project's code, even when it is imported with an alias.

    Example:

    ::

        import datetime

        # Setup
        patchers = patch_all_symbol_imports(datetime, 'my_project.', 'test')
        for patcher in patchers:
            mock_dt = patcher.start()
            # Do stuff with the mock

        # Teardown
        for patcher in patchers:
            patcher.stop()

    :param target_symbol: the symbol to search for imports of (may be a module,
        a function, or some other object)
    :param match_prefix: if not None, only search for imports in
        modules that begin with this string
    :param skip_substring: if not None, skip any module that contains this
        substring (e.g. 'test' to skip unit test modules)
    :return: a list of patchers for each import of the target symbol
    """

    patchers = []

    # Iterate through all currently imported modules
    # Make a copy in case it changes
    for module in list(sys.modules.values()):
        name_matches = (
                match_prefix is None
                or module.__name__.startswith(match_prefix)
        )
        should_skip = (
            skip_substring is not None and skip_substring in module.__name__
        )
        if not name_matches or should_skip:
            continue

        # Iterate through this module's locals
        # Again, make a copy
        for local_name, local in list(module.__dict__.items()):
            if local is target_symbol:
                # Patch this symbol local to the module
                patchers.append(mock.patch(
                    f'{module.__name__}.{local_name}', autospec=True
                ))

    return patchers

对于这个问题,可以使用以下代码:

from bigbad import request

patchers = patch_all_symbol_imports(request, skip_substring='test')
for patcher in patchers:
    mock_request = patcher.start()
    mock_request.return_value = 'mocked'

print(b.routine_b())

for patcher in patchers:
    patcher.stop()