QA
Mid-level QAFrameworks & Design PatternsFree question

How do you make automated tests independent of each other?

60-second answer

Each test should set up what it needs, run, and clean up, so it can run alone, in any order, and in parallel. I avoid dependencies between tests, like a test that relies on an order created by a previous test. Instead each test creates its own data, ideally through APIs or database seeding, which is faster than the UI, uses unique values like timestamped emails, starts with a fresh browser session, and deletes or isolates its data afterwards. Shared, read-only reference data is fine. This makes failures easier to debug and the suite parallelisable.

Explanation

Anti-patterns:

  • dependsOnMethods chains for long flows.
  • One shared test account used by all tests.
  • Tests relying on data left from yesterday's run.
  • Static variables carrying state between tests.

Techniques:

  • Builders/factories for test data (UserBuilder.aUser().withPremium().create()).
  • API setup in @BeforeMethod.
  • Cleanup in @AfterMethod(alwaysRun = true) or rely on a disposable environment.
  • Unique namespaces per run (run-20261106-1530-…).

Example

"Cancel order" used to depend on "Place order". Now it creates an order through the API in its setup, opens the order page directly, and cancels it, cutting the test from 45 to 12 seconds and removing a chain of skips.

Sample code

@Test
public void userCanCancelPlacedOrder() {
    User user = UserApi.createUser(Users.unique());           // own data
    String orderId = OrderApi.placeOrder(user, "SKU-101", 1);

    loginViaCookie(user);
    OrderPage page = new OrderPage(driver).open(orderId).cancel();

    Assert.assertEquals(page.status(), "Cancelled");
}

@AfterMethod(alwaysRun = true)
public void cleanUp() { UserApi.deleteCreatedUsers(); }