Sometimes it is necessary to disable the real import of a Python module and replace the import result with a mock object.
pip install patch-importimport pytest
from patch_import import patch_import
@pytest.fixture
def aiohttp__import_fixture():
with patch_import('aiohttp') as mocked_aiohttp:
mocked_aiohttp.__version__ = '3.3.3'
yield mocked_aiohttp
class TestPatchImport:
def test_patch_import(self, aiohttp__import_fixture):
"""An example test with patch_import"""
# patch_import__fixtures.py imports aiohttp which does not present in a project environment
# thus, we need to disable real import and replace aiohttp for a mock object
from patch_import.patch_import__fixtures import MyClass # please look at src/patch_import/patch_import__fixtures.py
assert MyClass.version() == '3.3.3'