首先,我的项目目录的相关部分如下所示:
└── my_package
├── my_subpackage
│ ├── my_module.py
| └── other_module.py
└── tests
└── my_subpackage
└── unit_test.py
我正在编写一些测试unit_test.py,需要在模块级别模拟外部资源。我想使用pytest fixture模块级范围并pytest 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.'
相关分类