Selective testing for Rust
tuist cargo test can skip the tests your change can't affect. It asks Tuist which tests the change since a recent full run can reach, from the code coverage each test recorded on earlier runs, and runs only those.
Coverage-based test selection is in early access. Contact us to enable it for your account. Until it's enabled, every test runs.
Requirements#
- The Rust project setup
- Code coverage turned on: Tuist selects tests by the coverage earlier runs uploaded
- Tests on Rust's standard test harness (libtest), with its default captured output
- Your project in a Git repository
- Full test runs, often enough. A run with selection turned off executes every test and becomes a baseline. A selective run compares against a recent one in its commit's history, and applies no coverage without one. We recommend running them on every commit of your default branch: a full run there also catches, before a change ships, what coverage can't see, such as a fixture or a service a test depends on.
Setup#
Set the coverage strategy in the workspace's root Cargo.toml, next to coverage:
[workspace.metadata.tuist.test_insights.coverage]
upload = true
[workspace.metadata.tuist.selective_testing]
strategy = "coverage"
If you configure Tuist in a tuist.toml instead, add [selective_testing] with strategy = "coverage" there.
Then run your tests as usual:
tuist cargo test --workspace
Turn selection off for the runs that should execute every test, such as your default branch's CI job:
TUIST_TEST_SELECTIVE_TESTING=0 tuist cargo test --workspace
Tracked files#
Some files affect tests without showing in coverage, such as fixtures and resources a test reads. List them in tracked_files, as globs relative to the workspace root: a change to one runs every test. A pattern that starts with ! leaves files out, including ones Tuist tracks by default.
[workspace.metadata.tuist.selective_testing]
strategy = "coverage"
tracked_files = ["tests/fixtures/**", "!**/*.md"]
- Tuist's defaults match at any depth of the repository:
Cargo.toml,Cargo.lock, build scripts (build.rs), Cargo configuration files (.cargo/config,.cargo/config.toml) and toolchain pins (rust-toolchain,rust-toolchain.toml,.tool-versions,mise.toml).**/Cargo.lockmatches the lockfile of your workspace, of any directory above it and of every other project in the repository. - Your patterns, exclusions included, are relative to your workspace root. With the workspace at
cli/,tests/fixtures/**trackscli/tests/fixtures/**, and!**/*.mdstops tracking Markdown files undercli/only.
How it works#
With coverage on, tuist cargo test runs each test of a test binary in a process of its own, so the coverage it records belongs to that one test. Before running them, it asks Tuist which tests your change can affect, and prints what it decided:
Tuist: coverage-based test selection: 12 of 340 tests selected, 328 skipped
Nothing selected in 2 test binaries: store, store::integration
- Tests Tuist didn't select are reported as filtered out, the way Cargo reports tests a name filter left out. The list of skipped tests never goes on Cargo's command line, however long it is.
- New tests, tests that failed last time, tests in a changed test file, and tests without recorded coverage always run.
- Doctests always run: they record no coverage, so Tuist can't tell what they depend on.
- Your test name filters,
--skip,--ignoredand--include-ignoredapply first, as in Cargo. Exit codes, output order and fail-fast behave as withcargo test. - Selection compares against runs of the same target triple. Runs labelled with
TUIST_SCHEMEcompare against runs with the same label. - With test sharding, the planning command (
tuist cargo test --no-run --shard-*) selects once and plans only the test targets with something selected, and every shard skips the rest. - Quarantined tests keep being muted or skipped as before.
Sometimes coverage isn't applied, and the output says why: every test runs. That happens without a recent baseline, when the change touches more than 5,000 files, or when a file that can affect any test changed, such as a Cargo.toml, Cargo.lock, a build script or one of your tracked files. A changed file that no test's coverage includes and no pattern tracks selects no tests, and an uncommitted change makes the run fall back to every test. Selection never fails your run: if Tuist can't be reached, every test runs.
- Selection applies only to commands whose test output Tuist can read: quiet (
-q), verbose (-v), alternate formats (--format json,--message-format), uncaptured output (--nocapture,--show-output) and options of the test harness other than filters,--skip,--exact,--ignored,--include-ignored,--test-threadsand--colorrun every test. A target with a custom test harness (harness = false) always runs whole. - A target runner of your own, set in a Cargo configuration file (
target.<triple>.runner) or throughCARGO_TARGET_<TRIPLE>_RUNNER, takes the place of Tuist's, and every test runs. - Each test runs in a process of its own when coverage is on, as it would with
--exactper test. A test that relies on state another test of the same binary left in its process, such as a static counter, behaves as if it ran alone. - Coverage only sees the code a test executes, including programs it starts from your package, such as
CARGO_BIN_EXE_<name>. A test whose real inputs are fixtures, resources, the network or the environment can break without any covered code changing, which is why full runs on your default branch matter.
A selective run's coverage report leaves out the tests it skipped. Tuist's code coverage carries their coverage forward from an earlier run instead, so the figure for the commit stays whole.