# How to Test Against Multiple Python Versions Using uv


A library that works on Python 3.11 can break on 3.12 because of removed deprecations, C API changes, or subtle differences in standard library behavior. Catching these failures before your users do requires running tests against each version you support. [uv](https://pydevtools.com/handbook/reference/uv.md) makes this straightforward: it downloads Python versions on demand, so there is no need to install them yourself.

## Prerequisites

- [uv installed](https://pydevtools.com/handbook/how-to/how-to-install-uv.md) on your system
- A Python project with [pytest](https://pydevtools.com/handbook/reference/pytest.md) configured as a dependency (see [Setting up testing with pytest and uv](https://pydevtools.com/handbook/tutorial/setting-up-testing-with-pytest-and-uv.md))

## Run Tests Against a Single Alternate Version

Pass `--python` to select a specific Python version:

```bash
uv run --python 3.12 pytest
```

If Python 3.12 is not already installed, uv downloads and installs it automatically. This works with any CPython version uv supports.

## Run Tests Against Multiple Versions

Loop over the versions you need:

```bash
for version in 3.11 3.12 3.13 3.14 3.15; do
    echo "=== Python $version ==="
    uv run --python $version pytest
done
```

If you use a [justfile](https://just.systems), add a recipe:

```just
test-all-pythons:
    #!/usr/bin/env bash
    for version in 3.11 3.12 3.13 3.14 3.15; do
        echo "=== Python $version ==="
        uv run --python $version pytest || exit 1
    done
```

Run it with:

```bash
just test-all-pythons
```

## Wait for a patched upcoming pre-release

CPython 3.16.0a1 is affected by [CVE-2026-115557](https://www.cve.org/CVERecord?id=CVE-2026-115557), so do not add it to the test matrix. Check the [eMudhra incident update checklist](https://pydevtools.com/blog/emudhra-ca-compromise-python-updates.md) and wait for a 3.16 release whose notes include the fix.

List every published build before adding the next minor version:

```console
$ uv python list --all-versions
```

Once a patched build appears, pass its full version to `uv run --python` so the test does not silently move to another pre-release. Expect failures during pre-release testing because dependencies may lack wheels for the new Python version and either build from source or refuse to install.

## Constrain Which Versions to Test

The `requires-python` field in `pyproject.toml` declares which Python versions your project supports:

```toml
[project]
requires-python = ">=3.11"
```

uv respects this constraint when resolving dependencies. If you ask uv to run against a version outside this range, it will refuse with an error explaining the conflict. Keep `requires-python` in sync with the versions you test against.

## Alternatives for Complex Test Matrices

A shell loop works well for running the same tests across a few Python versions. For more complex scenarios (testing with different dependency combinations, running in parallel, or generating reports), dedicated test automation tools are a better fit:

- [tox](https://pydevtools.com/handbook/reference/tox.md) with the [tox-uv](https://pydevtools.com/handbook/reference/tox-uv.md) plugin integrates tox's matrix support with uv's fast dependency resolution
- [nox](https://pydevtools.com/handbook/reference/nox.md) gives you a Python-scripted session runner that pairs well with uv through [nox's uv backend](https://nox.thea.codes/en/stable/config.html#configuring-a-session-s-virtualenv)

## See Also

- [Setting up GitHub Actions with uv](https://pydevtools.com/handbook/tutorial/setting-up-github-actions-with-uv.md) for running multi-version tests in CI
- [How to run tests using uv](https://pydevtools.com/handbook/how-to/how-to-run-tests-using-uv.md) for basic test execution
- [uv documentation on Python versions](https://docs.astral.sh/uv/concepts/python-versions/)
