QA AND TEST AUTOMATION · PART 3 · AUTOMATION FUNDAMENTALS · PRACTICE · 34 OF 112

Test Data Management: Builders, Seeds, Synthetic Data

Why test data decides whether automation is reliable, and how to manage it: known seed data and resets, creating data per test through the API, unique values so parallel tests never collide, builders and factories for readable setup, synthetic data with Faker, and the privacy rules for production copies. With ShopRung's reset endpoint and a Java test data builder.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Known seed + reset
  • create per test via the API
  • unique values
  • builders
  • seeded synthetic data
  • mask real data

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.

Two tests, one diary
  • 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.

ShopRung reset
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.

Own your data
// 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.

Never collide
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.

A test data builder
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.

Generated data
# 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.

Privacy rules
  • 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.

Cleaning up
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1Why is test data management important for automation?

Tests need a known starting state and data no other test changes; shared or changing data causes order-dependent, flaky failures.

Q2How do you make tests independent of each other's data?

Reset to seed data before tests, or have each test create its own data (ideally via the API) with unique values, and clean up afterwards.

Q3Why create test data through the API instead of the UI?

It is much faster and more reliable; the UI should only be used for the behaviour under test.

Q4What is a test data builder?

An object that builds test data with sensible defaults and a fluent API, so each test sets only the fields that matter to it.

Q5How do you use production data safely in testing?

Avoid it where possible; if needed, mask or anonymise personal data and remove sensitive identifiers, following privacy law such as GDPR. Prefer synthetic data.

Q6What is synthetic test data?

Generated, realistic but fake data (for example with Faker or Datafaker), useful for volume and variety; seed the generator so runs are reproducible.

Q7How do you avoid collisions when tests run in parallel?

Give every test its own data with unique values (UUIDs or prefixes) and avoid shared accounts or global state.

Q8Which card numbers should you use in payment tests?

The test card numbers published by the payment provider for its test mode, never real cards.