pytest_test

Python tests run with pytest.

Start Here#

The selected python interpreter must be able to import pytest. When the attribute is omitted, Struo first uses the workspace virtual environment. It then finds the installed pytest command and uses the interpreter named by that command or a neighboring Python interpreter. You can also set an absolute or workspace-relative interpreter path explicitly. A common direct installation is:

bash
python3 -m pip install pytest

Retrieve the runnable starter when you want a complete declaration and sample tests:

bash
struo query example pytest_test pytest-test-minimal --format json

Copy the declaration below into struo.toml, adjust srcs for the project, then establish discovery and run the file batches:

bash
struo test tests --format json struo query test-manifest tests --format json struo test tests --jobs 4 --format json

The first command runs the complete target. Later runs can use the resulting manifest for automatic batching and exact selection. See Testing and Scheduling for affected plans, case-level batching, and exact unit commands.

Description#

pytest_test invokes pytest through the selected Python interpreter, records stable node identifiers, and writes normalized Struo results. A complete run discovers cases for exact interactive execution and automatic scheduling.

Pytest collects from the declared source directories using its native ignore rules, including collect_ignore. Only files matched by srcs may be collected; exact case selection happens after native collection and cannot re-include an ignored module. Include nested conftest.py files and other collection inputs in config or data so changes invalidate discovery and cached results.

Configuration discovery uses the declared source paths, so package-local pytest settings are preserved. Native ignore hooks run before Struo enforces the source boundary; returning False from a project hook cannot admit an undeclared test file.

The target kind declares both Python and pytest as tool requirements. This lets schema discovery and toolchain setup explain the complete runtime requirement even when pytest is installed outside the workspace.

Automatic batching uses one batch per test file by default. Set batching to case for one batch per discovered case, or target to keep one batch for the target. A missing or stale manifest runs the whole target once before Struo uses smaller batches.

Attributes#

AttributeTypeRequiredDefaultDescription
pythonstringnoautomaticPython interpreter name, absolute path, or workspace-relative path that can import pytest
configlist<string>nopyproject.toml, pytest.ini, conftest.pyConfiguration and collection inputs
datalist<string>no[]Runtime data inputs read by tests
argslist<string>no[]Additional pytest arguments
coverage_per_testboolnofalseRun each current case in an isolated process using coverage.py
coverage_sourceslist<string>no[]Product Python source globs to instrument; required with coverage_per_test
envmap<string, string>no{}Explicit test environment variables
env_inheritlist<string>no[]Host environment variables inherited by name
batchingstringnofileAutomatic granularity: file, case, or target
labelslist<string>no[]Labels exposed through test discovery
timeout_msintnoemptyOptional timeout in milliseconds

Dependency Edges#

deps names targets whose changes should select this test target.

Providers and Capabilities#

The target emits struo_test_info and exposes test with the default, test_results, and logs output groups.

It also exposes test-discovery with the test_inventory output group. This uncached action collects the current native node identifiers without executing test bodies or using retained test results. Native ignore rules, configuration and declared source boundaries still apply. A collection error does not publish an inventory.

Set coverage_per_test = true and declare product paths in coverage_sources to collect attributed observations for opt-in approximate selection. The chosen interpreter must import both pytest and coverage.py. Collection, measurement and evidence assembly bypass the action cache. Every current case runs separately; selected runs and skipped or failed cases cannot establish complete replacement checkpoints. Starting a subprocess makes coverage incomplete because the parent measurement does not cover its work. Uninstrumented paths always retain graph-selected work. Test, data, configuration, interpreter, collector and environment identities bind the coverage history scope.

Example#

toml
[[target]] name = "tests" kind = "pytest_test" srcs = ["tests/**/*.py"] [target.attrs] python = ".venv/bin/python" labels = ["unit"]

Declare fixtures, configuration, and runtime files through srcs, config, or data so they participate in caching and manifest freshness.