Python monkeypatch.setattr() 在模块范围内使用 pytest fixture

Python monkeypatch.setattr() with pytest fixture at module scope

首先,我项目目录的相关部分如下所示:

└── my_package
    ├── my_subpackage
    │   ├── my_module.py
    |   └── other_module.py
    └── tests
        └── my_subpackage
            └── unit_test.py

我正在 unit_test.py 中编写一些测试,这些测试需要在模块级别模拟外部资源。我想使用具有模块级范围的 pytest fixturepytest monkeypatch 来完成此操作。这是我在 unit_test.py:

中尝试过的片段
import unittest.mock as mock
import pytest
from my_package.my_subpackage.my_module import MyClass


@pytest.fixture(scope='function')
def external_access(monkeypatch):
    external_access = mock.MagicMock()
    external_access.get_something = mock.MagicMock(
        return_value='Mock was used.')
    monkeypatch.setattr(
        'my_package.my_subpackage.my_module.ExternalAccess.get_something',
        external_access.get_something)


def test_get_something(external_access):
    instance = MyClass()
    instance.get_something()
    assert instance.data == 'Mock was used.'

一切正常。但是当我尝试将第 8 行从 @pytest.fixture(scope='function') 更改为 @pytest.fixture(scope='module') 时,出现以下错误。

ScopeMismatch: You tried to access the 'function' scoped fixture 'monkeypatch' with a 'module' scoped request object, involved factories
my_package\tests\unit_test.py:7:  def external_access(monkeypatch)
..\..\Anaconda3\envs\py37\lib\site-packages\_pytest\monkeypatch.py:20:  def monkeypatch()

有谁知道如何使用模块级范围进行 monkeypatch 吗?

如果有人想知道,这也是这两个模块的样子。

my_module.py

from my_package.my_subpackage.other_module import ExternalAccess


class MyClass(object):
    def __init__(self):
        self.external_access = ExternalAccess()
        self.data = None

    def get_something(self):
        self.data = self.external_access.get_something()

other_module.py

class ExternalAccess(object):
    def get_something(self):
        return 'Call to external resource.'

我找到了 this issue 指引了方向。我需要对模块级范围的解决方案进行一些更改。 unit_test.py 现在看起来像这样:

import unittest.mock as mock

import pytest

from my_package.my_subpackage.my_module import MyClass


@pytest.fixture(scope='module')
def monkeymodule():
    from _pytest.monkeypatch import MonkeyPatch
    mpatch = MonkeyPatch()
    yield mpatch
    mpatch.undo()

@pytest.fixture(scope='module')
def external_access(monkeymodule):
    external_access = mock.MagicMock()
    external_access.get_something = mock.MagicMock(
        return_value='Mock was used.')
    monkeymodule.setattr(
        'my_package.my_subpackage.my_module.ExternalAccess.get_something',
        external_access.get_something)


def test_get_something(external_access):
    instance = MyClass()
    instance.get_something()
    assert instance.data == 'Mock was used.'