Notes
The problem
Here is the classic failure. ShopRung's seed data has three Bug Hunter's Diaries. Test A buys all three. Test B adds one to a cart and expects success, but gets four hundred and nine, out of stock. Run B alone and it passes. Run it after A and it fails. Tests that depend on each other's data are order dependent, and they look flaky. The fix is a data strategy, not a retry.
- seed: The Bug Hunter's Diary, stock 3
- test A: buys 3 → stock 0
- test B: adds 1 to the cart expects success → 409 "out of stock"
- B passes alone, fails after A order-dependent = flaky
Seed and reset
Strategy one: seed and reset. Load known seed data, and reset to it before each test or each suite. ShopRung has an endpoint for this: post to API slash test slash reset, and it rebuilds the database with three users, twelve products, three coupons and Alice's two orders. Call it in a before each hook and every test starts from the same place. Real products often provide this through a test only endpoint, a database snapshot, or a fresh container.
POST /api/test/reset
→ {"status":"reset"}
drops all tables, reloads:
3 users, 12 products,
3 coupons, 2 of Alice's
orders
@BeforeEach → reset()
(serial runs; see parallel) Create per test
Strategy two: each test creates the data it needs. Instead of sharing Alice, the test signs up its own customer and fills its own cart, through the API, which takes milliseconds instead of a minute of UI clicks. Nobody else touches that customer, so other tests cannot break it. This is the most important rule for UI tests too: set up through the API, test through the UI. Only the thing under test should go through the browser.
// arrange: a fresh customer
POST /api/auth/signup
{"email":"t-8f3a@test.dev",
"name":"Test Buyer",
"password":"Passw0rd1"}
POST /api/cart/items
{"productId":1,"quantity":4}
// fast: no UI clicks to set up
// isolated: nobody else uses it Unique values
When tests create data, make it unique. A fixed email like test at test dot dev works the first time, then fails with four hundred and nine, email taken, because rule account four allows one account per email. Add a random part, like the first eight characters of a UUID. Unique values let tests run twice, run in parallel, and run in a shared environment without colliding. Prefix them, like t dash, so test data is easy to find and clean.
String email = "t-" + UUID
.randomUUID().toString()
.substring(0, 8) + "@test.dev";
# Python
email = f"t-{uuid4().hex[:8]}@test.dev"
ACC-4: one account per email
→ a fixed email fails on the
second run: 409 email_taken Builders
Setup code can bury the point of a test. A test data builder fixes that. A cart with four notebooks and save ten reads in one line: a cart, with four notebooks, coupon save ten, build. The builder fills sensible defaults for everything else, so the test shows only what matters to it. This is the builder pattern from object oriented design, and it is one of the most useful patterns in test frameworks. Python uses factory functions or libraries like factory boy.
Cart cart = aCart()
.with(4, NOTEBOOK)
.coupon("SAVE10")
.build();
static CartBuilder aCart() {
// sensible defaults:
// alice, empty, no coupon
}
// the test shows only what
// matters: 4 notebooks, SAVE10 Synthetic data
Synthetic data is realistic but fake: generated names, emails, addresses and dates, from libraries like Faker in Python or Datafaker in Java. It is great for volume and variety, like a thousand customers for a load test, or long, accented and unusual names that real users have. One tip: seed the generator with a fixed number, so the same data appears on every run and a failure can be reproduced. Random data without a seed creates flaky tests.
# Python: Faker from faker import Faker fake = Faker() fake.name(), fake.email(), fake.street_address() // Java: Datafaker seed the generator so a failure can be reproduced: Faker.seed(42)
Production data
Be careful with production data. Copying real customer data into a test system puts personal data at risk, and privacy laws like the GDPR apply to it. If a team must copy production, the data must be masked first: names, emails, phone numbers and addresses replaced, card and ID numbers removed. Better still, generate synthetic data that matches production's shapes and sizes. And never use real card numbers: payment providers publish test numbers for exactly this.
- real data in test systems = personal data at risk (GDPR and similar laws)
- if you must copy production: mask or anonymise names, emails, phones, addresses remove card and ID numbers
- prefer synthetic data that matches production shapes
- never real cards: use the provider's test numbers
Clean up
Clean up after yourself. On your laptop, a reset between tests is simplest. In a shared environment, delete what the test created in an after each hook, and never delete data you did not create. In CI, many teams start a fresh database or container for each run, which is the cleanest of all. Tagging test data with a prefix or a special email domain makes it easy to find and sweep up anything a crashed test left behind.
- reset between tests (local, serial), or delete what you created in @AfterEach (shared env), or use a fresh database or container per run (CI)
- tag test data: t- prefix, @test.dev, to sweep later
- never delete data you did not create
Common mistakes
Data habits that make suites flaky. Shared accounts that every test edits. Fixed emails and names that collide on the second run. Setting up data through the user interface, which is slow and fragile. Tests that rely on data an earlier test created. Random data without a seed. Real customer data in a test system. And hard coded ids, like product seven, that point at different things in different environments. Look up data by a stable key, like the sku.
- shared accounts all tests edit
- fixed emails and names
- setting up data through the UI (slow, fragile)
- tests relying on data from earlier tests
- random data without a seed
- real customer data in test
- "magic" IDs that differ per environment
Say it like this
In the interview, say: every test needs a known starting state and data no other test touches. I reset to seed data where possible, create test specific data through the API with unique values so tests can run in parallel, use builders to keep setup readable, use seeded synthetic data for volume, use masked production data only when really needed, and clean up whatever my tests create.
- "Every test needs a known
- state and data nobody else
- touches. I reset to seed data, create test-specific data
- through the API with unique
- values, use builders for readable setup, seeded
- synthetic data for volume, masked data only if needed, and clean up what I create."