在模块范围内带有pytest固定装置的Python Monkeypatch.setattr()

时间:2018-12-28 20:12:48

标签: python scope pytest fixtures

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

└── 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()

有人知道如何在模块级范围内进行猴子补丁吗?

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

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.'

1 个答案:

答案 0 :(得分:1)

我发现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.'