Welcome to pytest-missing-modules’ documentation#
Pytest fixture that can be used to create missing_modules contexts. |
|
|
Context manager generator that raises |
- missing_modules(monkeypatch)[source]#
Pytest fixture that can be used to create missing_modules contexts.
- Parameters:
monkeypatch (
MonkeyPatch) – A monkeypatch fixture, provided bypytest.- Return type:
- Returns:
A context manager that can be used to create missing modules contexts.
Examples
This first examples shows the most basic usage of this module.
The following must be placed in a test file.#import pytest def test_missing_numpy(missing_modules): with missing_modules("numpy"): with pytest.raises(ImportError): # Will always raise an error, even if NumPy is installed import numpy
A more interesting example would be to check that your package can still be imported, even if a dependency is missing.
The following must be placed in a test file.#import importlib import importlib.util import pytest import my_package # This succeeds def test_missing_dependency(missing_modules): with missing_modules("plotly", patch_find_spec=False): # We check that Plotly is installed assert importlib.util.find_spec("plotly") is not None # .. but not importable with pytest.raises(ImportError): import plotly # We check our package can still be imported importlib.reload(my_package)
- class MissingModulesContextGenerator(monkeypatch)[source]#
Context manager generator that raises
ImportErrorfor specified modules.In the provided context, an import of any modules in that list will raise an
ImportError.- Parameters:
monkeypatch (
MonkeyPatch) – The monkeypatch object used to perform all patches.
- __call__(*names, error_msg="Mocked import error for '{name}'", patch_import=True, patch_find_spec=True)[source]#
Enter the context manager.
- Parameters:
names (
str) – A list of modules names.error_msg (
str) – A string template for import errors.patch_import (
bool) – Whether to patchimportandimportlib.import_module.patch_find_spec (
bool) – Whether to patchimportlib.util.find_spec.
- Yields:
A monkeypatch instance that mocks imports of the specified modules.
- Return type: