Randomness In Tests (Don't Do It)
summary
Tests that use global randomness cause flakiness: kernel rand and Faker produce nondeterministic values, Postgres ID sequences leak state, and assigning Time.zone mutates a global. Bisect stalls when every test advances the same PRNG. Use deterministic seeds and dependency-injectable PRNGs instead — seed the PRNG per test run, pass Random instances into sample/shuffle, or use FactoryBot sequences. Property-based tools like prop_check or approval testing replace ad hoc sampling. If randomness remains, minimize scope and make it explicit.
AI-written summary · about
chapters (14)
- 0:00Introduction
- 1:03Rewrite tests
- 3:30Random test order
- 4:58Bad randomness examples
- 13:24Trivial vs non-trivial randomness
- 13:47Global state and time zones
- 15:33Database ID based feature toggling
- 16:55Faker for test data
- 19:50Bisect and PRNG leakage
- 21:54Using Random class
- 22:57Best practices
- 23:40Shift from Rails
- 26:33Critique of Rails
- 27:57Migrating Rails apps








