pytest#

pytest is a powerful testing framework for Python. It supports unit tests, doctests, and advanced features like fixtures and plugins. In this template, pytest is run with:

hatch run test
hatch run test-no-cov

Configuration#

Pytest settings are in the [tool.pytest.ini_options] section of pyproject.toml]. Doctest integration and coverage are enabled by default.

Extended Testing#

To step up in the game, an extended test environment and the command hatch run test:extended are available to verify the package on different Python versions and under different conditions thanks to the pytest plugins:

  • pytest-randomly that randomizes the test order;

  • pytest-rerunfailures that re-runs tests to eliminate intermittent failures;

  • pytest-xdist that parallelizes the test suite and reduce runtime, to help the previous points that increase the workload;

  • The file pyproject.tomlincludes configuration for them.

Further Reading#

Alternatives#

Other Python testing frameworks include:

  • unittest: Built-in Python testing framework.

  • nose2: Successor to nose, compatible with unittest.

  • doctest: Test code examples in docstrings.