1

我有一些实用程序功能src/utils/helper.py

想象一下,我有一个func_a在 utils/helper.py 中调用的函数,它在我的项目中的多个地方使用。

每次我使用它时,我都会像这样导入它

from src.utils.helper import func_a

现在我想func_a在我的测试中模拟这个。

我想在 conftest.py 中创建一个夹具,这样我就不需要为每个测试文件一次又一次地编写模拟函数。

问题是,在我的模拟函数中,我不能这样写。

https://pypi.org/project/pytest-mock/

mocker.patch('src.utils.helper.func_a', return_value="some_value", autospec=True)

我必须为每个测试文件这样写

mocker.patch('src.pipeline.node_1.func_a', return_value="some_value", autospec=True)

根据文档https://docs.python.org/3/library/unittest.mock.html#where-to-patch

由于我正在导入,因此func_afrom src.utils.helper import func_a必须模拟使用它的位置而不是定义它的位置。

但是这种方法的问题是我无法在 conftest.py 的夹具中定义它

目录结构

├── src
│   ├── pipeline
│   │   ├── __init__.py
│   │   ├── node_1.py
│   │   ├── node_2.py
│   │   └── node_3.py
│   └── utils
│       ├── __init__.py
│       └── helper.py
└── tests
    ├── __init__.py
    ├── conftest.py
    └── pipeline
        ├── __init__.py
        ├── test_node_1.py
        ├── test_node_2.py
        └── test_node_3.py
4

1 回答 1

5

好吧,正如您所写,如果您使用from xxx import. 您的第一个选择当然是在生产代码中使用完整的模块导入:

node_1.py

import src.utils.helper

def node_1():
    src.utils.helper.func_a()

我相信你知道这一点,但我还是想提一下。

如果您不想更改生产代码,则必须根据您所写的修补模块进行修补。这基本上意味着您必须动态构建补丁位置。如果您对测试函数和测试函数有对称命名,您可以执行以下操作:

conftest.py

@pytest.fixture
def mock_func_a(mocker, request):
    node_name = request.node.name[5:]  # we assume that the test function name is "test_node_1" for testing "node_1"
    module_path = f'src.pipeline.{node_name}.func_a'
    mocked = mocker.patch(module_path,
                          return_value="some_value",
                          autospec=True)
    yield mocked

如果无法从测试本身导出补丁路径,则必须向测试函数添加更多信息。这可能只有在您想做的不仅仅是夹具中的补丁时才有意义 - 否则您也可以直接添加patch装饰器。
您可以添加具有模块路径的自定义标记,或模块路径的一部分作为参数:

test_node_1.py

@pytest.mark.node("node_1")
def test_node(mock_func_a):
    node_1()
    mock_func_a.assert_called_once()

conftest.py

@pytest.fixture
def mock_func_a(mocker, request):
    mark = next((m for m in request.node.iter_markers()
                 if m.name == 'node'), None)  # find your custom mark
    if mark is not None:
        node_name = mark.args[0]
        module_path = f'src.pipeline.{node_name}.func_a'
        mocked = mocker.patch(module_path,
                              return_value="some_value",
                              autospec=True)
        yield mocked

或者,如果您需要提供完整路径:

test_node_1.py

@pytest.mark.path("src.pipeline.node_1")
def test_node(mock_func_a):
    ...

conftest.py

@pytest.fixture
def mock_func_a(mocker, request):
    mark = next((m for m in request.node.iter_markers()
                 if m.name == 'path'), None)  # find your custom mark
    if mark is not None:
        node_name = mark.args[0]
        module_path = f'{node_name}.func_a'
        ...
于 2020-05-17T18:29:11.317 回答