Glossary · UI automation
Test isolation
Also called: isolated tests, test independence, independent tests
Definition
The property that each test sets up its own state and can pass on its own, in any order and in parallel, without depending on or affecting other tests.
An isolated test creates everything it needs, does not rely on anything another test left behind, and does not leave anything that changes another test’s result. If you can run any single test alone, run the whole suite in reverse order, or run it across eight parallel workers and get the same results, the suite is isolated. Isolation is what makes parallel execution and sharding safe.
Example
Not isolated: the second test depends on the first.
test('creates a project', async ({ page }) => {
await page.goto('/projects/new');
await page.getByLabel('Name').fill('Apollo');
await page.getByRole('button', { name: 'Create' }).click();
});
test('renames the project', async ({ page }) => {
await page.goto('/projects/apollo'); // fails if run alone or first
});
Isolated: each test creates its own data, with a unique name, through the API.
test('renames a project', async ({ page, request }) => {
const name = `proj-${crypto.randomUUID()}`;
const res = await request.post('/api/projects', { data: { name } });
const { id } = await res.json();
await page.goto(`/projects/${id}`);
// ...rename and assert
});
Playwright helps at the browser level: each test gets a fresh BrowserContext, so cookies, local storage and sessions never leak between tests.
Sources of leakage
- Shared accounts and records that tests modify, such as one “test user” whose settings every test changes.
- Static or global state in the test code, such as a singleton driver or a class-level variable.
- Database rows without cleanup, or tests that assert on counts of a table other tests also write to.
- External state such as feature flags, email inboxes, time and caches.
How to check
Run tests in a random order. JUnit 5 supports @TestMethodOrder(MethodOrderer.Random.class), and a random default order for every class can be set in junit-platform.properties. Run single tests on their own. Run the suite with more workers than usual. Any test whose result changes is not isolated.
Full isolation has a cost, especially when setup is slow. A common compromise is to share expensive, read-only data per worker while keeping anything a test writes unique to that test.