Skip to content

Testing

coola.testing

Provide pytest fixtures to skip or require tests based on the availability of optional dependencies (e.g. NumPy, pandas, PyTorch).

The fixtures are defined in coola.testing.fixtures. Import one from there and use it as a test decorator, e.g. from coola.testing.fixtures import numpy_available then @numpy_available skips the test unless NumPy is installed.

coola.testing.fixtures

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, so supporting a new dependency takes one line, plus the two names in __all__.

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.fixtures.requires

requires(
    is_available: bool, name: str, *, plural: bool = False
) -> 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. "PyTorch and CUDA".

required
plural bool

If True, the reason of the second mark uses "are" instead of "is" (for a name that lists several dependencies).

False

Returns:

Type Description
tuple[MarkDecorator, MarkDecorator]

The (available, not_available) marks. The first one skips the test if the dependency is not available, the second one skips it if the dependency is available.

Example
>>> from coola.testing.fixtures 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'