Skip to content

Smart Test Selection

codeknife test works out which tests can see your changes and runs only those. It finds the changed files, traces callers through the same graph that Blast Radius uses, and turns the affected tests into cargo, pytest, go test or npx commands. Use it after an edit when the full suite is too slow to run every time.

This demo crate has two modules and two tests, one per module, in a git repository with a clean working tree:

src/lib.rs
pub mod math;
pub mod other;
#[test]
fn test_math_path() {
assert_eq!(math::add(1, 2), 3);
}
#[test]
fn test_other_path() {
assert_eq!(other::zero(), 0);
}

Edit src/math.rs (without committing) and preview the plan:

Terminal window
codeknife test --dry-run --format agent --no-cache
echo $?
CODEKNIFE test status=dry-run changed=1 targeted=1 commands=1
[TEST] src/lib.rs::test_math_path
[CMD] cargo test -p smart test_math_path
0

Only test_math_path calls into math.rs, so only that test is selected. Run it:

Terminal window
codeknife test --format human --no-cache
echo $?
CODEKNIFE test status=pass changed=1 targeted=1 commands=1
[TEST] src/lib.rs::test_math_path
[CMD] cargo test -p smart test_math_path
0

After breaking add so the test fails:

Terminal window
codeknife test --format agent --no-cache
echo $?
CODEKNIFE test status=fail changed=1 targeted=1 commands=1
[TEST] src/lib.rs::test_math_path
[CMD] cargo test -p smart test_math_path
[FAIL] cargo test -p smart test_math_path | Compiling smart v0.1.0 (.../smart) | Finished `test` profile [unoptimized + debuginfo] target(s) in 0.08s | Running unittests src/lib.rs (target/debug/deps/smart-6c13fb46a95dc405) | error: test failed, to rerun pass `-p smart --lib`
1

The set of changed files is chosen in this order:

  1. With --changed: the files git reports as changed. If that set is empty, the plan is empty (see When nothing is selected).
  2. Without --changed: the files git reports as changed, if there are any.
  3. Otherwise (clean working tree, no git repository, or no changes against --since): every file in the code model. This selects every test CodeKnife knows about.

The changed-file list from git is:

  • every path in git status, including untracked files, plus
  • with --since <ref>, every path that differs between <ref> and the HEAD commit.

Deleted files are dropped because they no longer exist on disk.

On the same demo crate with a clean tree on a feature branch that has one commit touching src/other.rs:

Terminal window
codeknife test --dry-run --format agent --no-cache
codeknife test --dry-run --since main --format agent --no-cache
codeknife test --dry-run --changed --format agent --no-cache
CODEKNIFE test status=dry-run changed=3 targeted=2 commands=1
[TEST] src/lib.rs::test_math_path
[TEST] src/lib.rs::test_other_path
[CMD] cargo test -p smart
CODEKNIFE test status=dry-run changed=1 targeted=1 commands=1
[TEST] src/lib.rs::test_other_path
[CMD] cargo test -p smart test_other_path
CODEKNIFE test status=dry-run changed=0 targeted=0 commands=0

A test is selected when any of these is true:

  1. It lives in a changed file.
  2. It calls into a changed symbol, directly or indirectly. For each function, method or route in a changed file, CodeKnife walks the call graph upwards through every caller, with no depth limit, and collects the tests that call any function in that cone. At most 30 symbols from the changed files are traced; symbols beyond the first 30 still count for the other two rules.
  3. It is in a test file whose module imports a changed module, directly or through other modules. CodeKnife builds the reverse import graph and walks it from the modules of the changed files.

Which functions count as tests, and which files count as test files, depends on the language adapter. See Language Support.

Selected tests are listed as <file>::<test name>, sorted by file and then by name.

Selected tests are grouped by the language of the file they live in. Each group produces commands from these templates. All commands run in the repository root.

Language Command Notes
Rust cargo test -p <package> <test> When exactly one test in a package is selected. <package> is the name from the [package] table of the nearest Cargo.toml, searching upwards from the test file to the root.
Rust cargo test -p <package> When two or more tests in the same package are selected. This runs the whole package’s tests.
Rust cargo test <test> or cargo test When no Cargo.toml with a [package] name is found.
Python pytest -q <file> <file> ... One command listing every selected test file.
Go go test ./<dir> ./<dir> ... One command listing the package directories of the selected test files; a file in the root uses ..
TypeScript and JavaScript npx vitest run <file> ... When the root package.json text contains vitest.
TypeScript and JavaScript npx jest <file> ... Otherwise, when the root package.json text contains jest.
TypeScript and JavaScript npm test Otherwise. This runs the whole suite.

The Rust test filter is passed to cargo test as is, and cargo test treats it as a substring match on test names, so other tests whose names contain it also run.

The package.json check is a plain text search over the whole file, so vitest or jest anywhere in it (for example in a script or a description) counts.

A polyglot demo with one Python, one Go and one TypeScript test, no git repository (so every file counts as changed) and vitest in package.json:

Terminal window
codeknife test --dry-run --format agent --no-cache
CODEKNIFE test status=dry-run changed=6 targeted=3 commands=3
[TEST] gopkg/calc/calc_test.go::TestTriple
[TEST] tests/test_calc.py::test_double
[TEST] web/inc.test.ts::inc
[CMD] pytest -q tests/test_calc.py
[CMD] go test ./gopkg/calc
[CMD] npx vitest run web/inc.test.ts

With jest instead of vitest the last command becomes npx jest web/inc.test.ts; with neither it becomes npm test.

Situation Commands Result
The changed-file set is empty none Pass, with the plan note no changed files detected
Tests were selected the generated commands Pass or fail
No test selected, and at least one changed file is a source file in the code model the repository verify command (below) Pass or fail, with the plan note no direct test unit match; using fallback repo verify command
No test selected, and no changed file is a source file (for example only a Markdown file changed) none Pass
No test selected, a source file changed, but no verify command can be detected none Pass

The fallback command is the first match at the repository root:

File at the root Command
Cargo.toml cargo check --workspace --all-targets
tsconfig.json npx tsc --noEmit
package.json npm test
go.mod go test ./...
pyproject.toml or pytest.ini pytest -q

On the Rust demo, adding a new untested file src/lonely.rs:

Terminal window
codeknife test --dry-run --format agent --no-cache
CODEKNIFE test status=dry-run changed=1 targeted=0 commands=1
[CMD] cargo check --workspace --all-targets

The plan notes are only shown in JSON output, not in the text output.

--test-cmd "<command>" replaces the whole command list with that one command. Selection still runs and the selected tests are still listed, but they are not passed to your command. The override applies even when nothing was selected or no files changed:

Terminal window
codeknife test --changed --test-cmd 'echo custom runner' --format agent --no-cache
echo $?
CODEKNIFE test status=pass changed=0 targeted=0 commands=1
[CMD] echo custom runner
0

Each command runs as sh -c "<command>" in the repository root, one after another. All commands run even if an earlier one fails. Their output is captured, not streamed to your terminal.

A command fails when it exits non-zero or cannot be started. The failure detail is the last five lines of stderr, or of stdout when stderr is empty, joined with |. A missing tool shows up as a shell error such as sh: 1: pytest: not found.

With --dry-run, nothing runs and the status is dry-run.

Flag Default Effect
--dry-run off Plan only; do not run any command. Always exits 0.
--test-cmd <CMD> none Run this command instead of the generated ones.
--changed off Use only git-changed files; never fall back to all files.
--since <REF> none Add files that differ between <REF> and HEAD to the changed set.
--no-cache off Do not read or write the .codeknife/ cache.
--path . Repository root.
--format auto json and sarif print the JSON outcome; every other format prints the text form below.
--strict off Accepted; it does not change selection.
--max-items none Accepted; not used by test output.

Text (every format except json and sarif):

CODEKNIFE test status=<dry-run|pass|fail> changed=N targeted=T commands=C
[TEST] <file>::<test>
[CMD] <command>
[FAIL] <command> | <detail>

JSON (--format json; sarif prints the same JSON):

{
"plan": {
"changed_files": [
"src/math.rs"
],
"targeted_tests": [
"src/lib.rs::test_math_path"
],
"commands": [
"cargo test -p smart test_math_path"
],
"notes": []
},
"passed": true,
"failed_commands": []
}

Each entry of failed_commands is a two-element array: the command and the failure detail.

Code When
0 Dry run, or every command passed, or there was nothing to run.
1 At least one command failed.
2 An error, for example an unparseable codeknife.toml. The message is printed to stderr as codeknife error: ....
  • Selection is file-based: every function in a changed file is treated as changed, not only the lines you edited.
  • Only the first 30 functions, methods and routes in the changed files are traced through the call graph.
  • Rust commands for two or more selected tests in one package run the whole package; JS/TS without vitest or jest in package.json runs npm test.
  • Commands are built from the test files’ languages only. There are no per-ecosystem settings in codeknife.toml for them; use --test-cmd to run something else.
  • The command output is not shown unless a command fails, and then only its last five lines.
  • MCP tool codeknife_test with the arguments path, no_cache, changed, since, dry_run and test_cmd. Unlike the CLI, dry_run defaults to true there. See MCP Tools.
  • codeknife verify runs the same selection and commands as its test stage. See Verify Pipeline.
  • codeknife blast shows the caller cones and covering tests behind the selection. See Blast Radius.