2.2 KiB
2.2 KiB
Test Fixtures — Derived Rules
Derives from
domains/testing/first-principles.mdP7 (Realism), P2 (Independence), P3 (Determinism).
Fixtures are Real Data (P7 Realism)
- A fixture resembles production data in shape, distribution, and edge cases.
- A fixture with
name: "test"andemail: "a@b.c"hides bugs that real data surfaces. - Use realistic names, realistic emails, realistic dates.
"Jane Doe", "jane.doe@example.com", "2026-03-15".
Factory Over Fixture (P2 Independence, P3 Determinism)
- A fixture file is shared state. A factory is fresh state per test.
- Prefer factories (e.g.,
factory.User()returning a new instance) over shared fixture files. - A shared fixture is mutated by one test, breaks another. Independence is violated.
Builders for Complex Data
- A builder (
UserBuilder().withEmail().withAdmin().build()) composes only the fields the test needs. - A builder with defaults: every field has a sensible default; tests override only what they test.
- A builder is the test's API to data. Stable, composable, readable.
Setup and Teardown (P2 Independence)
- Every test cleans up after itself. No test leaves state for the next.
setUp/tearDown(orbeforeEach/afterEach) restore the world.- A test that depends on the order of execution is not independent.
Determinism (P3)
- No
Date.now(), noMath.random()in fixtures. Inject the clock, inject the RNG. - A fixture that uses "now" is non-deterministic. It passes today and fails tomorrow.
- Fix timestamps:
createdAt: new Date("2026-01-01T00:00:00Z").
Edge Case Fixtures (P9 Edge Case Coverage)
- A fixture set includes: the empty case, the single-item case, the max-size case, the unicode case.
- A fixture set includes invalid data: malformed email, negative age, future date.
- Edge case fixtures are first-class, not "extra credit."
What Violates Fixture Discipline
| Violation | Principle |
|---|---|
name: "test" fixture |
P7 Realism |
| Shared fixture file mutated across tests | P2 Independence |
createdAt: new Date() (now) in fixture |
P3 Determinism |
| No edge-case fixtures | P9 Edge Case Coverage |
| A 500-line fixture file | P3 (complexity) |