Community코딩 & 개발github.com

flaky-test-triage

Diagnose and fix flaky (intermittently failing) tests. Use when a test "sometimes fails", passes on retry, fails only in CI or only in parallel, depends on run order, time, or randomness, or when the user wants to quantify flakiness, quarantine a test, or find the root cause of a nondeterministic failure.

flaky-test-triage란 무엇인가요?

flaky-test-triage is a Claude Code agent skill that diagnose and fix flaky (intermittently failing) tests. Use when a test "sometimes fails", passes on retry, fails only in CI or only in parallel, depends on run order, time, or randomness, or when the user wants to quantify flakiness, quarantine a test, or find the root cause of a nondeterministic failure.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/terusangki/agent-skills/tree/HEAD/skills/flaky-test-triage

Installed? Explore more 코딩 & 개발 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

Flaky test triage

Never "fix" a flaky test by adding a retry or a longer sleep without finding the cause. Process: reproduce → measure → classify → isolate → fix → prove.

1. Collect evidence first

  • Exact test ID, failure message, and stack from CI (for GitHub Actions: gh run view <run-id> --log-failed; see ci-failure-debugger).
  • History: how often, since when? gh run list --workflow <wf> --limit 50 --json conclusion,headSha,createdAt and correlate with git log around the first failure.
  • Environment differences CI vs local: CPU count, OS, TZ, locale, parallelism flags, DB/service versions, test order/sharding, network access.

2. Reproduce and measure

python <skill-dir>/scripts/rerun.py -n 30 -- pytest "tests/test_orders.py::test_ship" -q -p no:randomly
python <skill-dir>/scripts/rerun.py -n 40 -j 4 -- go test ./orders -run TestShip -count=1
python <skill-dir>/scripts/rerun.py -n 30 --shell -- npx jest src/cart.test.ts -t "coupon"     # --shell for npx on Windows
python <skill-dir>/scripts/rerun.py -n 20 --env TZ=Asia/Tokyo --env LANG=de_DE.UTF-8 -- <cmd>

The report gives pass rate, failure signatures (normalized, grouped), pass vs fail duration, and timeouts. Exit code 1 if any run failed. --save-dir flaky-logs keeps every failing log.

If it never fails in isolation, escalate pressure in this order: run the whole file, then the whole suite (order dependence), then -j 4 (shared state/races), then CPU stress (stress-ng --cpu 0 or run alongside a build), then the CI container image locally (docker run the same image).

Order dependence tools: pytest -p randomly / --randomly-seed=<seed> and pytest --lf; Jest --runInBand vs default, --randomize --seed=<n>; Go -shuffle=on (prints the seed), -count=1 disables cache; RSpec --order rand:<seed> + --bisect; JUnit 5 @TestMethodOrder(Random).

3. Classify (match symptoms → cause)

SymptomLikely causeFix
Fails only with other tests / specific orderShared mutable state: globals, singletons, env vars, DB rows, files, caches, mocked modules not restoredIsolate per test (transactions/rollback, unique temp dirs, reset singletons, restore mocks in teardown)
Fails only in parallelPort/file/DB name collisions, race conditionsEphemeral ports (:0), unique names per worker, proper locking/awaiting
Failing runs much slower / timeoutsFixed sleep, waiting on async work, slow CIWait on a condition (poll with deadline), fake timers, raise timeout only for genuinely slow ops
Around midnight, month end, DST, specific TZReal clock / local timezoneInject clock / freeze time (freezegun, jest.useFakeTimers().setSystemTime), use UTC
Different values each runUnseeded randomness, unordered collections (set/dict/map iteration, SQL without ORDER BY)Seed RNG, sort before asserting, ORDER BY
Float assert mismatchFloating point accumulation orderApprox compare (pytest.approx, toBeCloseTo)
Network errors / 429Real external callsMock at the HTTP boundary, or recorded fixtures
UI test: element not found / detachedAsserting before render; animationsAuto-waiting locators (await expect(locator).toBeVisible()), disable animations
Async test passes but logs errors laterUn-awaited promise/goroutine leaks into next testAwait everything; leak detectors (--detectOpenHandles, goleak)
Only on CI OSPath separators, case-sensitive FS, line endings, localepathlib/path.join, normalize EOL, explicit locale

4. Isolate the cause

  • Add temporary diagnostics around the failing assertion (actual values, timestamps, thread/worker id) and re-run with rerun.py --save-dir.
  • For order dependence, bisect the preceding tests: run [first half of preceding tests] + target, halve until one polluter remains.
  • git bisect if it started failing at a known point (use rerun.py -n 20 as the bisect predicate; exit code 1 = bad).

5. Fix and prove

  • Fix the root cause in the test or the code (flaky tests often expose real race conditions in production code — check that first).
  • Prove it: rerun.py with at least 3× the runs that previously showed a failure (e.g. failed 3/30 → require 0/100), under the same pressure (parallel/order) that reproduced it.
  • If you can't fix now: quarantine with an explicit marker + linked issue (@pytest.mark.skip(reason="flaky: #123"), test.skip with issue link), and tell the user. Never silently delete or retry-wrap.

6. Report template

## Flaky test: tests/test_orders.py::test_ship
- Before: 27/30 pass (90%), signature `a1b2c3d4`: AssertionError: expected 'shipped' got 'pending'
- Root cause: test asserts before the background job completes; job runs on a thread pool, slower under CI load.
- Fix: wait on job completion event with 5s deadline instead of `sleep(0.1)` (tests/test_orders.py:44).
- After: 100/100 pass, -j 4 (rerun.py).
- Production impact: none / <describe if the race exists in prod code>

Individual skills in this repo

This repo contains 2 individual skills — each has its own dedicated page.

관련 스킬