Agent Wranglers

TST-56Recommended

Run the tests a change affects on every pull request

On a pull request, run the tests that cover the changed files. Run the whole suite on every merge to the main branch and before every deploy.

How to do it

A suite past its time budget slows every change, and parallel runs only go so far. Let the tools pick tests from the import graph, such as jest --changedSince or --findRelatedTests, pytest-testmon, or the affected commands in Nx, Bazel and Gradle. Keep a list of files that run everything, such as the lock file, test helpers, settings and the CI file. Run the whole suite when the change touches any of them, or when the tool cannot work out the graph. Never let the selection decide what ships. The deploy runs the full suite, so a missed test fails before production. Track how often the main branch fails after a green pull request, and widen the selection when it happens.

Examples

JavaScript example

import { execSync } from 'node:child_process';

const RUN_EVERYTHING = [/^package-lock\.json$/, /^jest\.config\./, /^test\/helpers\//, /^\.github\//];

const changedFiles = execSync('git diff --name-only origin/main...HEAD', { encoding: 'utf8' }).split('\n').filter(Boolean);
const touchesShared = changedFiles.some(file => RUN_EVERYTHING.some(pattern => pattern.test(file)));
const command = touchesShared ? 'npx jest' : 'npx jest --changedSince=origin/main';
console.log(`Running: ${command}`);
execSync(command, { stdio: 'inherit' });

Python example

import re
import subprocess

RUN_EVERYTHING = [r"^uv\.lock$", r"^requirements.*\.txt$", r"^conftest\.py$", r"^tests/helpers/", r"^\.github/"]

changed_files = subprocess.run(
    ["git", "diff", "--name-only", "origin/main...HEAD"], capture_output=True, text=True, check=True
).stdout.split()
touches_shared = any(re.search(pattern, path) for path in changed_files for pattern in RUN_EVERYTHING)
command = ["pytest"] if touches_shared else ["pytest", "--testmon"]
print("Running:", " ".join(command))
subprocess.run(command, check=True)

How to check

Change a file that a test in another module imports. The pull request run includes that test and lists the selected tests in its log. Change the lock file. The run includes every test. Every deploy log shows a full suite run.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/testing/flaky-and-slow-tests/test-selection">Agent Wranglers testing playbook</a>. In code you write, add a comment next to it with the same link.