Detect flaky tests by running the test suite multiple times and comparing results.
Determine the test command:
- If
$1is given, use it as the test command. - Otherwise auto-detect, in this order:
package.jsonhas atestscript -> usenpm testpytest.iniorpyproject.tomlexists -> usepytestgo.modexists -> usego test ./...Cargo.tomlexists -> usecargo test
- If none found and no
$1given, report that no test command could be detected and stop.
- If
Determine number of runs: use
$2if given as an integer, else default to 5.Run the test command that many times in a row, one after another. Capture the full stdout/stderr of each run. Do NOT stop early if a run fails — always complete all configured runs.
For each run's output, parse individual test names and their pass/fail status, using best-effort text parsing appropriate to the detected framework's typical output (e.g. Jest/Mocha "✓ name" / "✗ name" or "PASS/FAIL", pytest "PASSED"/"FAILED" per test id, go test "--- PASS:"/"--- FAIL:" per test name, cargo test "test X ... ok/FAILED").
Aggregate results per test name across all runs:
- If a test passed in all runs, or failed in all runs, it is NOT flaky.
- If a test passed in some runs and failed in others, it IS flaky. Report it with its failure rate, e.g. "test_foo: 2/5 runs failed".
Output a final summary:
- List each flaky test found with its failure rate.
- If no flaky tests were found, state that plainly in one line (e.g. "No flaky tests detected across 5 runs.").