# Selective testing for Gradle {#selective-testing-for-gradle}

The Tuist Gradle plugin 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.

> [!IMPORTANT]
> **Early access**
>
> Coverage-based test selection is in early access. Contact us to enable it for your account. Until it's enabled, every test runs.

## Requirements {#requirements}

- The `dev.tuist` Gradle plugin applied, as described in <.localized_link href="/guides/get-started/gradle-project">Get started with a Gradle project</.localized_link>
- <.localized_link href="/guides/features/test-code-coverage/gradle">Code coverage</.localized_link> turned on: Tuist selects tests by the coverage earlier runs uploaded
- Tests on the JUnit Platform (JUnit 5, or JUnit 4 through Vintage), which record which files each test executes
- 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 {#setup}

Set the coverage strategy in the `tuist` block of your `settings.gradle.kts`, next to coverage:

```kotlin
tuist {
    coverage {
        enabled = true
    }
    selectiveTesting {
        strategy = "coverage"
    }
}
```

Turn selection off for the runs that should execute every test, such as your default branch's CI job:

```sh
TUIST_TEST_SELECTIVE_TESTING=false ./gradlew test
```

### Tracked files {#tracked-files}

Some files affect tests without showing in coverage, such as fixtures and resources a test reads. List them in `trackedFiles`, as globs relative to your root project: a change to one runs every test. A pattern that starts with `!` leaves files out, including ones Tuist tracks by default.

```kotlin
tuist {
    selectiveTesting {
        strategy = "coverage"
        trackedFiles.addAll(listOf("app/src/test/resources/**", "!**/*.md"))
    }
}
```

## How it works {#how-it-works}

Every build that runs test tasks, such as `./gradlew test`, `check` or `build`, asks Tuist which tests your change can affect before it runs them, and prints what it decided:

```text
Tuist: Coverage-based test selection: 12 of 340 tests selected, 328 skipped.
Tuist: No test selected in :feature:settings, so their test tasks are skipped.
```

- A project with no selected test has its test tasks skipped. Inside the other projects, the tests Tuist didn't select are excluded with test filters.
- New tests, tests that failed last time, tests in a changed test file, and tests without recorded coverage always run. A parameterized test runs whole whenever its method runs.
- The filters are part of each test task's build cache key, so a selective run never reuses the results of a run that selected other tests. An unchanged selection keeps the configuration cache.
- With <.localized_link href="/guides/features/test-sharding/gradle">test sharding</.localized_link>, `tuistPrepareTestShards` selects once and plans only the selected tests, and every shard skips the rest.
- Quarantined tests keep being 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 build script, a version catalog, `gradle.properties`, the Gradle wrapper or one of your [tracked files](#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 build: if Tuist can't be reached, every test runs.

> [!NOTE]
> **Limitations**
>
> Selection reads every project's configuration, so it's off in builds with Isolated Projects or configuration on demand. Coverage only sees the code a test executes: 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.

> [!TIP]
> **Test coverage**
>
> A selective run's coverage report leaves out the tests it skipped. Tuist's <.localized_link href="/guides/features/test-code-coverage/gradle">code coverage</.localized_link> carries their coverage forward from an earlier run instead, so the figure for the commit stays whole.
