TL;DR + Summary: Test fixtures with random values are hard to debug, and fixtures with hard-coded dates become stale. Generate stable, readable values that look real, and make them deterministic so failures reproduce.
Why random fixtures cost time
A test that fails with a random UUID in the logs is a test you cannot search for. A test that fails with a random timestamp is a test whose clock dependency you cannot see.
Randomness in fixtures feels realistic and undermines reproducibility. When a failure appears once in twenty runs, the randomness is often the reason.
The solution is deterministic generation: values that vary by input but are stable for the same input.
Deterministic UUIDs
A version 5 UUID is derived from a namespace and a name, which means the same name always produces the same UUID. That is exactly what a test fixture needs.
Derive the UUID from a readable seed such as the test name or the entity identifier. The UUID looks realistic, is stable across runs, and is traceable back to its source.
- Version 4 — random, good for production, poor for fixtures
- Version 5 — deterministic from a name, ideal for tests
- Version 3 — also deterministic, MD5-based, older
- Version 7 — time-ordered, useful for sorting tests
Use version 5 for fixtures and keep version 4 for production identifiers. Mixing them is a source of confusion when debugging.
Ordering and sort tests
If a test depends on creation order, UUIDs must be ordered. Version 4 values sort arbitrarily, which makes an order-based test flaky.
Use a sequential identifier or version 7 UUIDs for those cases. Alternatively, sort by an explicit timestamp field rather than by the identifier.
Timestamps that do not go stale
A fixture with a hard-coded date in 2023 becomes “two years old” and starts breaking tests that check recency. The test was fine; the data aged.
Generate timestamps relative to a fixed base date, and inject the clock. Tests that use the real clock are tests that will fail at some point for no code-related reason.
For display tests, a fixed absolute date is correct. For behaviour tests, a relative offset from an injected now is correct. Decide which one each test needs.
A fixture should be realistic and reproducible. Random is only one of those.
Names and text that look real
Placeholder text such as “Test User 1” makes screenshots and layout checks meaningless, because real names have different lengths and character sets.
Use a small fixed pool of realistic names with varied lengths, including at least one with an accent and one with a hyphen. That catches layout and encoding issues early.
Avoid using real people’s data. A pool of invented names with realistic shapes gives most of the benefit without the risk.
Referential integrity
Fixtures that reference each other must be consistent. An order that points to a missing customer is a bug in the fixture, and it will produce confusing test failures.
Generate the graph in dependency order and derive foreign keys from the parent’s generated identifier rather than typing them by hand.
A small fixture builder function is usually clearer than a large static file, because it makes the relationships explicit.
When to use a real sample
Sometimes only real data reveals the problem: unusual character sets, very long fields, or malformed historical records. Keep a small, sanitised sample for those cases.
Sanitise thoroughly. Replace names, addresses, and identifiers, and check that the replacement is consistent across related records.
Keep the sample small. A ten-thousand-record fixture is slow to load and rarely needed; a hundred well-chosen records covers most cases.
Tooling
Most languages have a library for deterministic fixture generation, and using it is more reliable than writing your own.
For ad-hoc work, generating values by hand is fine as long as the rules are consistent. For converting or inspecting identifiers during debugging, masterstool is a quick way to check formats and encodings.
Good fixtures are boring: readable, stable, and obviously related to each other. Boring fixtures produce debuggable tests, which is the only property that matters.
Fixture builders versus static files
A static fixture file is easy to read and hard to vary. A builder function is harder to read and easy to vary, which matters as soon as a test needs a slightly different case.
Start with a builder that takes only the fields a test cares about and derives everything else. The test then states its intent in one line rather than restating twenty defaults.
Derive the identifier from the test’s distinguishing input, so two fixtures in the same test cannot accidentally share an identifier. Accidental collisions produce failures that look like application bugs.
Keeping fixtures small
A fixture with forty fields is a maintenance burden and a signal that the underlying entity is doing too much. Keep the builder focused on the fields that affect behaviour.
If a field is required by validation but irrelevant to the test, set it once in the builder default rather than in every call site. The default becomes documentation of the minimum valid entity.
Review the builder when the schema changes. A builder that silently permits an invalid entity will produce fixtures that pass validation in tests and fail in production, which is the worst possible outcome.
Documenting the fixture contract
Write a short comment above the builder explaining what is guaranteed and what a test must specify. That comment is the difference between a fixture that is reused and one that is copy-pasted.
Include an example in the comment. An example communicates the contract faster than a description of it.
Review the comment when the builder changes. A stale contract is worse than none, because it invites trust it no longer deserves.