Skip to main content

Python unit testing utility. Patches imports

Project description

patch-import

Tests codecov Mypy

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

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'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

patch_import-1.0.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

patch_import-1.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file patch_import-1.0.0.tar.gz.

File metadata

  • Download URL: patch_import-1.0.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for patch_import-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9aaa16c607718cf1377ff3e23c4af282b09b05136ee3d47ae0e7a056b5316a12
MD5 b23519bdda7a1de22f70a331f0691561
BLAKE2b-256 0e5e3d8d88a16ae971f4d6ab547ff4997f25feedd1429d17708903e3c86f7e3c

See more details on using hashes here.

File details

Details for the file patch_import-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: patch_import-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for patch_import-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae2ed7770946254a8df77052f68033286c0360103363293e02d5f57d296b227e
MD5 0a6565d9e13dbfcfb639580c0f79d2c8
BLAKE2b-256 3513e9e7aedfd0d3219a3b3fca197a5a596cdb9cd3d472fd2e87bd08198702d4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page