Skip to content

Lesson 9 of 9 · 24m

Fast CI Suites and Flaky-Test Control

Split suites by cost, parallelize safely, manage coverage overhead, and eliminate flaky tests before the team learns to ignore red builds.

A slow suite is not a neutral inconvenience. It changes behavior: developers batch larger changes, skip local runs, ignore intermittent failures, and merge before feedback arrives. Fast tests are a workflow feature. Flaky tests are worse — they train the organization to distrust the signal entirely.

Split by cost and dependency, not folder tradition

Keep fast unit tests in the default developer path. Move database, HTTP, browser, and container-dependent tests into explicit jobs.

{"scripts": {"test": "phpunit --testsuite unit", "test:integration": "phpunit --testsuite integration", "test:all": "phpunit"}}

CI should still run everything. The point is that a developer editing src/Billing/PriceCalculator.php can get meaningful feedback in two seconds before starting the full suite.

Parallelize after isolation

PHPUnit itself runs serially. ParaTest is the standard parallel runner:

vendor/bin/paratest --processes=8 --runner=WrapperRunner

Parallel tests expose shared resources immediately. If tests write to storage/framework/cache, the same sqlite file, or the same uploaded filename, they will pass serially and fail randomly under ParaTest. Give each process its own database schema or temporary directory. Laravel's parallel testing support creates per-process databases for this reason.

Coverage is expensive; control it

Xdebug is accurate but slow. PCOV is usually much faster for line coverage. Install one coverage driver in CI, not both by accident:

vendor/bin/phpunit --coverage-clover build/clover.xml

Do not collect coverage in the mutation-testing run. Infection needs coverage information, but it can generate or consume it deliberately; blindly layering coverage collectors compounds runtime.

Treat flaky tests as defects

The response to a random failure should never be a silent retry. Capture evidence first:

  • random seed, test order, exact time, and timeout values
  • database rows before rollback and external-service request IDs
  • process number under parallel execution

PHPUnit's random execution order is useful because it reveals hidden state. A failure only under order #7421 means ordering matters. Reproduce with --order-by=random --random-order-seed=7421 rather than rerunning until green.

Retries are acceptable at the CI orchestration boundary for known infrastructure flakes, but quarantine the test and file a defect. Do not retry whole suites until they pass; that turns one broken test into a slow success ritual.

Keep CI boring and observable

Cache Composer dependencies keyed by composer.lock. Warm migrations once per job. Split unit, integration, static analysis, and browser checks into parallel jobs rather than serial stages. Publish JUnit XML so the CI provider can report the slowest and flakiest tests.

vendor/bin/phpunit --log-junit build/junit.xml --do-not-cache-result

Then look at duration weekly. Suites rarely become slow all at once; they accumulate two-second tests until nobody remembers where the ten minutes went.

Field notes

  • Never use sleep() as synchronization. Poll for a condition with a deadline instead.
  • Freeze time in logic tests; use real short timeouts only where timeout behavior itself is tested.
  • Parallelization cannot fix quadratic tests. Fix the slow test before adding workers.
  • A green suite everyone ignores is functionally worse than a red suite people trust.

Was this lesson helpful?

Sign in to rate