Tests often create data as they run: a new account, record, or product item. Without a reliable way to remove that data, it can build up in your environment long after a test finishes. To keep a test self-contained, you can mark a step group or flow at the end of the test as a teardown. A teardown runs every time the test runs, even when the test fails, so the data your test creates gets cleaned up either way.
This article explains how to clean up test data with a teardown, including how to:
- Mark a step group or flow as a teardown
- Write cleanup steps that always run
- Choose between a teardown and a cleanup stage
Mark a step group or flow as a teardown
Any step group or flow can act as a teardown, as long as it's the final step in the test. To mark one as a teardown:
- Open a test in the mabl Trainer on the latest version of the mabl Desktop App.
- Add the step group or flow you want to use for cleanup as the last step in the test.
- Hover over the step group or flow and click on … (More actions).
- Select Run as teardown group or Run as teardown flow.
The teardown option only appears when the step group or flow is the final step in the test. If you don't see it, check that no other steps come after it.
A step group marked as a teardown
Write cleanup steps that always run
A good teardown removes exactly the data the test created, and nothing else. A reliable way to do that is to capture the identifier of the data as the test creates it, store it in a variable, and use that variable in the teardown to target the same record for removal.
For example, a test that creates a customer account might extract the new account ID from the page URL and store it in a variable. Later, the teardown then reads that variable and deletes the matching account, so each run cleans up after itself no matter which account it created.
Because a teardown runs even when the test fails, this pattern keeps a mid-test failure from leaving data stranded. If the test fails after creating the account but before finishing, the teardown still runs and removes the account.
Write cleanup steps, not assertions
A teardown is meant to run to completion, so a failure inside it fails the test, the same way it would in a framework like Playwright or Jest. Write teardown steps that clean up reliably rather than assertions that might fail. If you need to validate something about the data you created, do it in the main body of the test, before the teardown.
Choose between a teardown and a cleanup plan stage
Teardown step groups and flows live inside the test that created the data, making them the right choice when a single test needs to clean up after itself. The cleanup travels with the test, so there's no separate teardown test or plan stage to build and maintain.
For cleanup that spans several tests, or that has to run regardless of which test created the data, a dedicated cleanup stage in a plan is the better fit. You can build a stage of cleanup tests and turn on the Always run stage setting so that the stage runs even when an earlier stage fails. For more on structuring plans this way, see organizing tests into plans.