Selective testing for Elixir
The Hex package can skip the tests your change can't affect. Before mix test runs, it asks Tuist which tests the change since a recent full run can reach, from the code each test executed 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 shared Elixir setup, which routes
mix testthrough the package - Code coverage with
--cover: the runs that collect coverage also record which code each test executed, which is what Tuist selects by - 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 tuist options of your mix.exs:
def project do
[
tuist: [selective_testing: [strategy: :coverage]]
]
end
Then run your tests with coverage, as you already do to upload it:
mix test --cover
Turn selection off for the runs that should execute every test, such as your default branch's CI job. They still record what each test executed for later runs:
mix tuist.test --cover --no-selective-testing
Tracked files#
Some files affect tests without showing in coverage, such as fixtures a test reads. A change to a tracked file runs every test. Tuist tracks these by default, at any depth of the repository:
mix.exsandmix.lockconfig/**andtest_helper.exspriv/repo/migrations/**, since a migration changes the schema every database test runs against.tool-versions,mise.tomland.mise.toml
Add your own in tracked_files, as globs relative to your mix.exs. A pattern that starts with ! leaves files out, including ones Tuist tracks by default:
def project do
[
tuist: [
selective_testing: [strategy: :coverage, tracked_files: ["test/fixtures/**", "!config/dev.exs"]]
]
]
end
- Tuist's defaults match at any depth of the repository:
**/mix.lockmatches themix.lockof your project, of any directory above it and of every other project in the repository. - Your patterns, exclusions included, are relative to the directory of your
mix.exs. In a project atserver/,test/fixtures/**tracksserver/test/fixtures/**, and!**/mise.tomlstops tracking themise.tomlfiles underserver/only.
So an exclusion can't stop another project's files from matching a default. Use coverage paths for that.
Monorepos#
The default tracked files match at any depth, so in a repository that holds other projects, a change to another project's mix.lock or mise.toml runs every one of your tests. Name the files that belong to your project with coverage paths:
def project do
[
tuist: [
selective_testing: [strategy: :coverage],
coverage: [paths: ["**", "../shared/**"]]
]
]
end
- A changed file outside the paths selects no test and never runs every test.
- Tracked-file patterns, Tuist's defaults included, only match files inside the paths.
- The
libandprivdirectories of your path dependencies inside the repository are added for you.
A test that runs code outside your paths isn't selected when that code changes, so list the shared code your tests run. Without paths, the whole repository counts.
How it works#
Every mix test --cover run asks Tuist which tests your change can affect before it runs them, and prints what it decided:
Tuist: coverage-based test selection: 12 of 340 tests selected, 328 skipped
Nothing selected in 2 modules: MyApp.BillingTest, MyApp.MailerTest
- Test modules with no selected test are left out of the run. Inside the other modules, the tests Tuist didn't select are left out with filters.
- New tests, tests that failed last time, tests in a changed test file, and tests without recorded coverage always run.
- With test sharding,
mix tuist.test.buildselects once and plans only the selected tests, and every shard runs only those. - Quarantined tests keep being skipped as before.
- Umbrella roots aren't selected: run selection inside each application.
Sometimes coverage isn't applied, and the output says why: every test runs. That happens without a recent baseline or when one of your tracked files changed:
Tuist: coverage-based test selection not applied: tracked_file_changed (mix.lock), so every test runs.
A changed file that no test's coverage includes and no pattern tracks selects no tests. Selection never fails your run: if Tuist can't be reached, every test runs, with a warning.
A test run's Selective Testing tab on the dashboard shows each test's decision and why, the changed files, and in its Config tab every tracked-file pattern the run used.
Coverage only sees the code a test executes: a test whose real inputs are fixtures, the database, 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 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.