Testing
coola.testing ¶
Provide pytest marks and fixtures to test code that has optional
dependencies (e.g. NumPy, pandas, PyTorch).
The marks are defined in coola.testing.marks. Import one from there
and use it as a test decorator, e.g. from coola.testing.marks import
numpy_available then @numpy_available skips the test unless NumPy
is installed. The fixtures are defined in coola.testing.fixtures:
load them with pytest_plugins = ["coola.testing.fixtures"] in the
top-level conftest.py.
pytest is required: install coola with the testing extra
(pip install coola[testing]).
coola.testing.marks ¶
Define some pytest fixtures for testing.
pytest is required to use these fixtures.
Each optional dependency has a pair of marks: <name>_available skips
the test unless the dependency is available, and
<name>_not_available skips it if the dependency is available. They
are created by requires from one row of _DEPENDENCIES, so
supporting a new dependency takes one line.
Most marks are named after one package. The torch_cuda_*,
torch_mps_* and torch_numpy_* marks are named after a feature of
PyTorch or a pair of packages: they check that PyTorch is installed
and that the CUDA device, the MPS device or NumPy (in a compatible
version) is usable.
coola.testing.marks.requires ¶
requires(
is_available: bool, name: str
) -> tuple[MarkDecorator, MarkDecorator]
Create the pair of marks for a dependency.
Use it to support a new optional dependency: the marks are meant to be assigned to module-level names, so they can be imported and used as test decorators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_available
|
bool
|
Whether the dependency is available. |
required |
name
|
str
|
The name of the dependency in the skip reasons, e.g.
|
required |
Returns:
| Type | Description |
|---|---|
tuple[MarkDecorator, MarkDecorator]
|
The |
Example
>>> from coola.testing.marks import requires
>>> numpy_available, numpy_not_available = requires(True, "NumPy")
>>> numpy_available.kwargs["reason"]
'Requires NumPy'
>>> numpy_not_available.kwargs["reason"]
'Skip if NumPy is available'
coola.testing.fixtures ¶
Define a pytest fixture to isolate the default registries.
pytest is required to use this fixture.
coola.testing.fixtures.isolated_default_registries ¶
isolated_default_registries() -> Generator[None]
Restore the default registries after the test.
A test that registers a type in a default registry (for example
with register_equality_testers) changes it for the rest of the
process. With this fixture, the test works on copies of the default
registries and the original ones are restored when it ends. Load
the module as a plugin in the top-level conftest.py
(pytest_plugins = ["coola.testing.fixtures"]) and use the
fixture with
pytest.mark.usefixtures("isolated_default_registries").
Yields:
| Type | Description |
|---|---|
Generator[None]
|
|
Example
>>> from coola.testing.fixtures import isolated_default_registries
>>> isolated_default_registries.__name__
'isolated_default_registries'