TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › Toolkit: hatch, tox & nox

Toolkit: hatch, tox & nox

Once a package builds, the next questions are "does it work on every Python version and dependency set we claim?" and "how do contributors run the checks?". Three tools answer them by creating and driving throwaway environments for you.

Tool Configured in Strength
tox tox.ini or [tool.tox] in pyproject.toml the classic test matrix runner; declarative; huge plugin ecosystem
nox noxfile.py (Python code) sessions are plain Python functions: loops, conditionals, anything
hatch [tool.hatch.envs.*] in pyproject.toml project manager + environments + matrix + build/publish, from the hatchling authors

All three build your package (sdist/wheel via PEP 517) and install it into each environment, so they test what users get rather than your source tree. All three can use uv to create environments much faster (tox-uv plugin, nox --default-venv-backend uv, hatch's installer = "uv").

tox

toml
# pyproject.toml, tox 4.21+ native TOML configuration
[tool.tox]
env_list = ["3.12", "3.13", "lint"]

[tool.tox.env_run_base]
dependency_groups = ["test"]
commands = [["pytest", "-q", { replace = "posargs", extend = true }]]

[tool.tox.env.lint]
skip_install = true
deps = ["ruff"]
commands = [["ruff", "check", "."]]

tox runs every environment in env_list; tox -e 3.13 -- -k parser runs one and passes arguments to pytest. tox creates each environment once and reuses it until dependencies change (tox -r recreates).

nox

python
# noxfile.py
import nox

nox.options.default_venv_backend = "uv"

@nox.session(python=["3.12", "3.13"])
def tests(session):
    session.install(".", "pytest")
    session.run("pytest", "-q", *session.posargs)

@nox.session
def lint(session):
    session.install("ruff")
    session.run("ruff", "check", ".")

@nox.session
@nox.parametrize("attrs", ["24.2.0", "26.1.0"])
def compat(session, attrs):
    session.install(".", "pytest", f"attrs=={attrs}")
    session.run("pytest", "-q")

nox -l lists sessions (tests-3.12, tests-3.13, lint, compat(attrs='24.2.0')…), nox -s lint runs one. The parametrised compat session is the kind of thing that's awkward in an INI file and trivial in Python: test against the oldest and newest version of a dependency your >= range allows.

hatch

toml
[tool.hatch.envs.default]
installer = "uv"
dependencies = ["pytest"]

[tool.hatch.envs.default.scripts]
test = "pytest -q {args}"

[[tool.hatch.envs.test.matrix]]
python = ["3.12", "3.13"]

hatch run test runs the script in the default environment; hatch test and hatch run test:test run the matrix. Hatch also does hatch build, hatch publish, hatch version minor and can install Python versions (hatch python install 3.13).

A runnable model: expanding a test matrix

What these tools do first is expand a matrix declaration into a list of named environments, each with a Python version and a set of pins. This model reproduces nox-style naming:

python · try it yourself
from itertools import product

pythons = ["3.12", "3.13"]
deps = {"attrs": ["24.2.0", "26.1.0"], "requests": ["2.31.0", "2.34.2"]}
skip = {("3.12", "attrs", "26.1.0")}       # e.g. a known-incompatible combination

envs = []
for py, combo in product(pythons, product(*[[(name, v) for v in vs] for name, vs in deps.items()])):
    if any((py, name, v) in skip for name, v in combo):
        continue
    label = ", ".join(f"{name}='{v}'" for name, v in combo)
    pins = [f"{name}=={v}" for name, v in combo]
    envs.append((f"compat-{py}({label})", pins))

for name, pins in envs:
    print(f"{name:<55} pip install . pytest {' '.join(pins)}")
print(len(envs), "environments")
compat-3.12(attrs='24.2.0', requests='2.31.0')          pip install . pytest attrs==24.2.0 requests==2.31.0
compat-3.12(attrs='24.2.0', requests='2.34.2')          pip install . pytest attrs==24.2.0 requests==2.34.2
compat-3.13(attrs='24.2.0', requests='2.31.0')          pip install . pytest attrs==24.2.0 requests==2.31.0
compat-3.13(attrs='24.2.0', requests='2.34.2')          pip install . pytest attrs==24.2.0 requests==2.34.2
compat-3.13(attrs='26.1.0', requests='2.31.0')          pip install . pytest attrs==26.1.0 requests==2.31.0
compat-3.13(attrs='26.1.0', requests='2.34.2')          pip install . pytest attrs==26.1.0 requests==2.34.2
6 environments

Matrices grow multiplicatively; that's why real projects test the edges (oldest supported pins, newest releases) rather than every combination, and run the full matrix nightly instead of on every push.

Test yourself

Why do tox, nox and hatch install your package into each environment instead of testing the source tree?

  1. It's faster
  2. To test the built package users will get, catching packaging mistakes like missing files
  3. Because pytest requires it
  4. To avoid needing a venv

When would you pick nox over tox?

  1. Never
  2. When session logic is easier to express as Python code (loops, parametrisation, conditionals)
  3. When you don't use Python
  4. Only for linting