简体   繁体   English

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

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

First of all, the relevant portion of my project directory looks like:首先,我的项目目录的相关部分如下所示:

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

I am writing some tests in unit_test.py that require mocking of an external resource at the module level.我正在unit_test.py中编写一些需要在模块级别模拟外部资源的测试。 I would like to use a pytest fixture with module level scope and pytest monkeypatch to acomplish this.我想使用具有模块级范围和pytest monkeypatchpytest fixture来完成此操作。 Here is a snippet of what I have tried in unit_test.py :这是我在unit_test.py尝试过的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.'

Everything works just fine.一切正常。 But when I try to change line 8 from @pytest.fixture(scope='function') to @pytest.fixture(scope='module') , I get the following error.但是当我尝试将第 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()

Does anyone know how to monkeypatch with module level scope?有谁知道如何使用模块级范围进行monkeypatch?

In case anyone wants to know, this is what the two modules look like as well.如果有人想知道,这也是这两个模块的样子。

my_module.py 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 other_module.py

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

I found this issue which guided the way.我发现了指导方式的这个问题 I needed to make a few changes to the solution for module level scope.我需要对模块级范围的解决方案进行一些更改。 unit_test.py now looks like this: 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.'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM