Prompt
How do I manage test data setup and teardown for UI tests?
Latest observation
A good UI-test data strategy is to make data setup and cleanup repeatable, isolated, and fast. The best approach depends on whether your tests run against a real backend, a test database, or mocked services, but the core ideas are the same.
Recommended patterns
1) Use dedicated test environments
- Run UI tests only against a separate test/staging environment
- Never share with production data
- Reset or refresh this environment regularly
2) Create data programmatically
Prefer creating test data through:
- API calls
- database fixtures/scripts
- factory helpers
- seed data loaders
This is usually more reliable than manually preparing data in the UI.
3) Make each test self-contained
Each test should:
- create the data it needs
- not depend on data from another test
- clean up after itself, if necessary
This prevents order-dependent failures.
4) Use setup and teardown hooks
Most test frameworks support hooks like:
beforeEach/afterEachbeforeAll/afterAll
Use them to:
- set up reusable baseline state
- delete temporary records
- reset cookies/local storage/session state
5) Prefer unique test data
Give every test-created entity a unique identifier:
- timestamp
- UUID
- test-run ID
This avoids collisions with parallel test runs.
6) Clean up through APIs, not the UI
If cleanup is needed, use:
- delete endpoints
- database cleanup scripts
- environment reset jobs
Avoid using the UI for teardown unless that’s what you’re specifically testing.
7) Reset state between tests
Depending on your app, reset:
- database records
- browser storage
- authentication state
- queues/jobs/emails if they affect results
Common teardown strategies
Full reset
Best when feasible:
- recreate database
- reload fixtures
- clear cache/storage
This gives the cleanest isolation.
Selective cleanup
Delete only what the test created:
- user accounts
- orders
- files
- messages
Useful when full resets are too expensive.
Ephemeral environments
For CI pipelines, spin up a fresh environment per run:
- new database
- new app instance
- destroy after tests finish
This is often the most robust option for integration-heavy UI tests.
Practical tips
- Keep data factories in a shared test helper module
- Make cleanup idempotent so it can run even if setup partially fails
- Log created resources to help debug failures
- If tests run in parallel, isolate by tenant/user/project rather than shared records
- Avoid relying on hardcoded IDs or existing records
Example approach
A typical flow:
-
beforeEach- create user via API
- create required records
- log in via API or UI
-
test runs
- perform UI interactions
- assert visible behavior
-
afterEach- delete created records
- clear browser storage/session
Rule of thumb
If setup/teardown feels complex, that’s often a sign your tests need:
- better isolation
- smaller scope
- more backend/API assistance for data management
If you want, I can also show:
- a Cypress example
- a Playwright example
- a Selenium/JUnit example
- a best-practice test data architecture for UI tests