EveryRole

Test data cleanup: leave the app as you found it

Test data cleanup: every run deletes what it made, and only that. How to mark what a test creates, when to clean, and what to do when the app has no delete.

Test data cleanup is one rule: a test run deletes what it made, and nothing else. Skip the first half and your test account fills with hundreds of “Sample item” rows until the tests start failing for no reason. Skip the second half and one day a test deletes something a person cared about.

Both halves are easy to say and easy to get wrong. Here is how we handle each.

What it should look like

Here is the clean-up step done by hand on our practice app, after a run that tested “an editor creates an item”. The account already had one item of its own. The run’s item carries the run’s mark.

The clean-up step, done by hand the way EveryRole does it: delete the run’s marked item, reload, check. The person’s item has no mark and is never touched.

One item in, one item out, and the list is exactly as it was. A person looking at the account afterwards can’t tell a test ran.

Why leftover test data breaks tests

Leftovers change what the next run sees.

  • Counts drift. A test expects “1 item” after creating one. With forty leftovers it sees “41 items” and fails, or worse, was written loosely enough to pass anyway.
  • Names collide. An app that wants unique names refuses a second “Sample item”. The create test now fails every run until someone cleans up by hand.
  • Limits get hit. A plan allows a set number of projects. The test account reaches it, and every create is refused.
  • The empty state disappears. You can never again test what a new user sees, because the account is never empty.
  • Pages slow down. A list of four thousand rows takes long enough to load that timing-sensitive tests turn into flaky tests.

Each of these looks like a bug in the app or a flaky test. People spend an afternoon on it before someone opens the list and sees the pile.

Cleaning up test data in E2E tests: four rules

1. Mark everything the run creates

You can only delete what you can recognise. So every thing a test makes carries a mark that says which run made it: a short id added to its name. Sample item 7f3a, where 7f3a belongs to this run and no other.

A fixed prefix like TEST- is not enough. It tells you a test made it, not which one, so two runs at the same time delete each other’s data mid-test.

2. Delete by the mark, never by a pattern

The dangerous clean-up is the broad one: “delete every item whose name starts with Sample”, or the worst version, “delete everything in the test account”. It works until the day someone points the tests at an account that has real data in it, or a real user names something “Sample budget”.

Delete the things carrying this run’s mark. If the run made two things, the clean-up deletes two things.

3. Use the app’s own delete, and check it worked

Clean up the way a user would: open the thing, press its “Delete”, confirm. Then reload and check it is gone.

This is slower than reaching into the database. It is also a free test of your delete feature on every run, and it can’t leave the half-deleted records that a direct database delete leaves when it skips the app’s own logic (files on disk, rows in other tables, search entries).

4. Clean up as its own step, even when the test failed

Clean-up at the end of a test is skipped exactly when it matters most: when the test fails halfway. Make it a separate stage that runs after the cases, pass or fail, and looks for anything with the run’s mark.

Give it its own result, too. “The test passed and the clean-up failed” is important to know and belongs in neither the pass column nor the fail column.

When the app has no delete

Plenty of things can’t be deleted through the page: a sent message, an order, an account, an audit entry. The honest options:

  • Say so. The run’s report lists what it left behind and where. This sounds weak. It is far better than silently piling up.
  • Reuse. One long-lived test record that tests edit and restore, instead of a new one each run.
  • Don’t create it on production. Run the cases that leave marks on staging or a preview, and run only the reading cases on the live app.
  • Add a delete for test data. If your app can create it, a small admin-only way to remove marked test records is a fair thing to build.

What you should not do is skip the test because the clean-up is awkward. Sign-up is the classic example: it creates an account every time, and it is the journey you can least afford to leave untested. Make the account, test it, then delete it through the app’s own “delete my account” if there is one, and report it if there isn’t.

Don’t forget the things that aren’t rows

A run also leaves: uploaded files, sessions, emails in a test inbox, items in a cart, a half-finished wizard that remembers its answers. Our practice app’s set-up wizard keeps what you typed on step one. A test that fills it in changes what the next test sees on that page.

Go through your journeys once and ask of each stop: after this, what is different? That list is your clean-up.

How EveryRole cleans up

EveryRole adds the run’s own id to the first text field of what a case creates, so each thing it makes can be told apart from the account’s own data and from other runs. Clean-up is its own stage after the cases. It deletes what that run made, and only that, through the app’s own delete control, then reloads to check it is gone. For uploads it does the same with the app’s remove control.

Two rules are fixed. It never deletes something the same run didn’t make. And when it can’t remove something (the app offers no delete), it says what was left on the app, instead of reporting a clean run.

While exploring your app it never presses “Delete”: that word is on the list of words it never clicks.

A ten-minute check

Open your test account now and look at its lists. If you find a pile of old test data, you have two jobs: clear it once by hand, then add the mark and the clean-up stage so it doesn’t come back.

If the lists are clean, check the other half. Find the line in your tests that deletes, and read what it matches. If it is a pattern, narrow it to the run’s own mark before it meets real data.

Keep reading

QA practice

Regression testing when you ship every day

Regression testing for small teams. A short suite of the journeys that already worked, run on every deploy. What goes in it and how often to run it.

5 min read

QA practice

QA for startups: a routine without a QA team

QA for startups with no tester. A routine a team of three can keep: what runs on every deploy, what a person does each week, and what to skip without guilt.

5 min read

QA practice

Risk-based testing: what to test first

Risk-based testing ranks what you test by what a break would cost and how likely it is. A two-question method for small teams, worked through on a real app.

6 min read