# Environments

Each tested version gets a virtual environment under `.pytest-uv/`, named after the version:

```text
.pytest-uv/
├── py3.11/
├── py3.12/
└── py3.13/
```

The directory is set by [`venv-dir`](<https://jirikuncar.github.io/pytest-uv/reference/configuration/index.md#venv-dir>) and is resolved from pytest's rootdir (the directory of the `pyproject.toml` or `pytest.ini` that configures pytest), so running pytest from a subdirectory uses the same environments. pytest-uv puts a `.gitignore` into it, so git ignores the whole directory.

Your project's `.venv` is not touched: pytest runs there as the controller, and only the workers use `.pytest-uv/`.

## With a `uv.lock`

When `uv.lock` exists next to `pyproject.toml`, each environment is created empty and then synced from it:

```bash
uv venv --clear --python 3.12 .pytest-uv/py3.12
UV_PROJECT_ENVIRONMENT=.pytest-uv/py3.12 uv sync --frozen --python 3.12 <sync-args>
```

- **Same versions as your `.venv`.** Every version installs what the lock file pins, resolved for that Python version.
- **`--frozen`.** uv installs the lock file as it is, without checking or updating it. Run `uv lock` (or `uv add`) after editing dependencies; `uv run` does that for you.
- **Default groups.** Like `uv sync`, this installs the project, its dependencies and uv's [default groups](<https://docs.astral.sh/uv/concepts/projects/dependencies/#default-groups>), which is `dev` unless `[tool.uv] default-groups` says otherwise.
- **[`sync-args`](<https://jirikuncar.github.io/pytest-uv/reference/configuration/index.md#sync-args>)** adds arguments, for example to install only the test group:

```toml
[tool.pytest-uv]
sync-args = ["--no-default-groups", "--group", "test"]
```

See [`examples/dependency-groups`](<https://github.com/jirikuncar/pytest-uv/tree/main/examples/dependency-groups>).

## Without a lock file

```bash
uv venv --clear --python 3.12 .pytest-uv/py3.12
uv pip install --python .pytest-uv/py3.12/bin/python <install-args>
```

[`install-args`](<https://jirikuncar.github.io/pytest-uv/reference/configuration/index.md#install-args>) defaults to the project in editable mode (`-e .`, or `-e .[test]` when the project has a `test` extra), plus `--group test` and `--group dev` for each of those dependency groups that `pyproject.toml` defines. Anything `uv pip install` accepts works:

```toml
[tool.pytest-uv]
install-args = ["-e", ".[test]"]                 # an extra instead of a group
# install-args = ["-e", ".", "-r", "requirements-test.txt"]
```

Without a lock file, each environment resolves dependencies on its own, so two versions may get different releases of the same package.

## pytest, pytest-xdist and pytest-uv

Every worker needs pytest and pytest-xdist, and pytest-uv itself for the [`python_version` fixture](<https://jirikuncar.github.io/pytest-uv/guide/version-specific-tests/index.md#the-python_version-fixture>). After the sync or install, pytest-uv checks the environment and installs whichever is missing: pytest-uv as the same version, or from the same path or URL, as the copy running the controller. When pytest-uv is in your `dev` group, as `uv add --dev pytest-uv` puts it, all three already come from your lock file. To pin pytest or pytest-xdist, list them in your dependencies or groups.

## Reuse and rebuilds

After building an environment, pytest-uv writes `.pytest-uv-stamp` into it with a fingerprint of:

- `pyproject.toml` and `uv.lock`,
- the requested version,
- `sync-args` or `install-args`.

It also records the interpreter's full version and its `sys.path` (see [Worker isolation](<https://jirikuncar.github.io/pytest-uv/guide/isolation/index.md>)). The next run reuses an environment whose fingerprint matches and whose recorded paths still exist, and rebuilds the others, so an interpreter that `uv python upgrade` replaced or `uv python uninstall` removed triggers a rebuild on its own. Editing your source code or tests does not change the fingerprint: the project is installed in editable mode, so workers import the current code.

To force a rebuild after changing something the fingerprint does not cover (environment variables such as `UV_INDEX_URL`, settings in `uv.toml`):

```bash
rm -rf .pytest-uv              # all versions
rm -rf .pytest-uv/py3.12       # one version
```

## Parallel builds and concurrent runs

Versions are prepared in parallel. Each environment is locked while it is being built, so two pytest runs started at the same time (two terminals, an editor and a terminal) do not build the same environment at once: the second waits, then reuses it.

## uv's output

uv's output is captured. When a step fails, for example because a package has no wheel for that version, pytest stops with a usage error that starts with `pytest-uv: command failed with exit code N:`, followed by the command and uv's output.

Run with `-v` to see uv's output as it happens, each line prefixed with the environment (`[py3.11] ...`).

pytest-uv runs the `uv` found on `PATH`, or the executable named by the `UV` environment variable. `--collect-only` skips building environments.
