Skip to content

shmakovpn/patch-import

Repository files navigation

patch-import

Tests codecov Mypy pypi downloads versions

Sometimes it is necessary to disable the real import of a Python module and replace the import result with a mock object.

Installation

pip install patch-import

Example

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

About

Python unit testing utility. Patches imports

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages