There are advantages to using the API to tear down automated end-to-end tests

Published on February 17, 2025

When I automate a test I create a clean starting position for each test so that the automated tests can run concurrently. “The establishment of a known-good starting position for the test before it is run, and its re-establishment at its conclusion, is vital to avoid cross-test dependencies”[1]. 

Re-establishing the start position of the test is often referred to as “tear down”, and can be done by interacting with the UI using a testing tool or framework. Playwright has AfterAll() and AfterEach() that can be used for tear down. 

If you want to reduce the time it takes for tests to run, you can use a Lean metaphor to approach the problem of how to save time on the tests. The metaphor describes a process as a river that you want to run smoothly and looks for the largest rock that slows the river. Teardown occurs on every test so teardown can be the large rock slowing the tests.

When using an automated end-to-end testing framework, it is tempting to interact with the UI to tear down at the end of a test. However, clicking through the UI to tear down can be brittle and take a long time.

Tear down does not need to run via the UI because it is not part of the end-to-end test. To reduce the time it takes for tests to run, you can use the API of the application being tested instead of interacting with the UI to tear down.  The API will run quicker than interacting with the UI, so it will save time on every test.

Using the API of the application under test for tear down has additional the advantage that it will be less brittle because it is unaffected by changes to the UI. 

Tearing down the tests via the API also enables the test automation engineer to learn part of the API of the application they are testing.

There are several advantages to using the API of the application under test to tear down, it is quicker, less brittle and the engineer learns how to use the API.

References:

[1] Continuous Delivery by Jez Humble and David Farley (2011, p337)