Module 3. Practice: from requirements to automated tests · Lesson 28 of 41 · 14 min
Test data and environments: servers, factories, mocks and feature flags
Why you need different environments, how to prepare and clean up data, when to mock external services and how to test behind feature flags.
Environments
| Environment | What for | Who manages the data |
|---|---|---|
| local | Development and debugging | The developer, seeds |
| dev | Daily builds, the first check | The team, automated tests |
| staging | Regression, acceptance testing, demos | QA, setup through the API |
| prod | Real users | Monitoring and synthetic checks only |
Before testing, always check which build is deployed — half of the "phantom" bugs turn out to be tests of the wrong version.
Test data: three rules
- Each test creates its own data — that keeps tests independent (see hooks in the Playwright lesson).
- Data is recognizable: a qa- prefix, a marker in the name, a separate email domain — easy to find and clean up.
- No real personal data from production. If you need realistic data, generate or anonymize it (GDPR, CCPA and HIPAA all apply to test copies too).
Valid SSN-format values, routing numbers, IBANs, phone numbers and emails come from the test data generator.
Factories
// helpers/factories.ts
let n = 0;
export const userFactory = (over: Partial<User> = {}): User => ({
email: "qa+" + Date.now() + "-" + n++ + "@example.com",
name: "Test User",
phone: "+1 (212) 555-0100",
...over,
});
A factory returns a valid object by default, and the test changes only the field it checks: userFactory({ name: "" }) for a negative case.
Mocks and stubs
External services (payments, SMS, shipping) are slow, paid or unavailable in tests. Options:
| Approach | When |
|---|---|
| The provider's sandbox | Staging, end-to-end payment checks (Stripe test mode, PayPal sandbox) |
| Network-level mock (MSW, page.route in Playwright) | Front-end UI tests |
| Mock server (WireMock, Prism) | API tests of a service without its dependencies |
| Contract test | A guarantee that the mock matches the real API (Pact) |
// Playwright: simulate the promo service being down
await page.route("**/api/promo/apply", (r) => r.fulfill({ status: 503, body: "{}" }));
await page.getByRole("button", { name: "Apply" }).click();
await expect(page.getByRole("alert")).toHaveText("Discounts are temporarily unavailable");
Feature flags
New features often hide behind a flag. A tester has to check both states (on and off), toggling without a restart, and that a disabled feature is really unavailable — including through the API.
Cleanup
Delete what you created in afterEach or with a nightly job by prefix. A cluttered staging is a source of false bugs and slow lists.
Key takeaways
- Check the deployed build first — that weeds out false bugs.
- Each test creates its own recognizable data and cleans it up.
- Mocks speed up tests; contracts guarantee the mocks match reality.
Check yourself
1. Can you copy a production database with personal data into staging?
2. How do you check the UI's behavior when the promo service is down?
3. What should you check for a feature behind a flag?