Skip to content

Testing

This template assumes test cases are written for the pytest framework.

Tip

Testing is a broad subject and is well covered by a number of sources - these are linked to in relevant sections.

Subject Link
Tutorial RealPython
Tips CodeCut
Tips CodeCut
Test Categories pyOpenSci
Test Categories Bite code!
Official Documentation pytest

Plugins

Area Plugin
Benchmarking pytest-benchmark
Test Coverage pytest-cov
Execution Order pytest-randomly
Mocking HTTP pytest-httpx

Libraries

Library Purpose Blog
tox Testing in Multiple Environments https://realpython.com/python-testing/#testing-in-multiple-environments
nox Testing in Multiple Environments https://dzone.com/articles/automating-python-testing-across-versions-with-tox-and-nox
moto AWS Mocks https://aws.amazon.com/blogs/devops/unit-testing-aws-lambda-with-python-and-mock-aws-services/
hypothesis Property-based Testing https://hypothesis.works
mutmut Mutation Testing https://deployed.pl/blog/mutation-testing-in-python

Codecov

Codecov integration is automated by the release.yaml workflow.

For this to work, either pass in a CODECOV_TOKEN secret directly or define it at the GitHub repository/organisation/enterprise levels.

jobs:
  option-1:
    uses: jambazid/gha-actions/.github/workflows/python-uv.yaml@main
    secrets: inherit # `CODECOV_TOKEN` defined as a repository/organisation/enterprise scoped secret.
    with: {}
  option-2:
    uses: jambazid/gha-actions/.github/workflows/python-uv.yaml@main
    secrets:
       CODECOV_TOKEN: ${{ secrets.SOME_CODECOV_TOKEN }} # Explicit injection.
    with: {}
  option-3:
    uses: jambazid/gha-actions/.github/workflows/python-uv.yaml@main
    with:
      codecov-oidc: true # OIDC

Tox

This template integrates tox with multiple Python interpreters managed by uv.

Simply specify your Python environments as py3XX (where XX is the two-digit Python minor version) and tox will provision them [ephemerally] via uv:

# Option 1 - Taskfile:
task tox

# Option 2 - uv:
cd ${PYTHON_PACKAGE_DIR} && uv run tox;

Below is the required pyproject.toml configuration:

# pyproject.toml

[dependency-groups]
test = [
  "tox >= 4.25, < 5",
  "tox-uv >= 1.33.4, < 2",
]

[tool.tox]
requires = ["tox >= 4.25"]
envlist = [
  "py311",
  "py312",
  "py313",
  "py314",
]

[tool.tox.env_run_base]
runner = "uv-venv-lock-runner"
commands = [
  ["pytest", "tests/", "--import-mode", "importlib"],
]

Tip

The tox-uv plugin manages environments by dispatching Python and package installation to uv via the uv-venv-lock-runner.

You can then enable tox in CI via a boolean flag:

jobs:
  release:
    uses: jambazid/gha-actions/.github/workflows/python-uv.yaml@main
    with:
        tox: true
        ...