Skip to main content

What is Test Isolation?

Test isolation ensures that each test runs independently without affecting or being affected by other tests. This is critical for reliable, maintainable test suites.
Isolated tests can run in any order, in parallel, and produce consistent results.

Why Isolation Matters

Reliability

Tests don’t fail due to side effects from other tests

Parallelization

Tests can run simultaneously without conflicts

Debugging

Failures are easier to debug when tests are independent

Maintainability

Tests can be modified without breaking others

Browser Context Isolation

Playwright uses BrowserContext for test isolation:
From client/browserContext.ts:58-174, each context provides:
  • Independent cookies
  • Separate localStorage/sessionStorage
  • Isolated cache
  • Independent network state
  • Separate permissions

Test Isolation Pattern

The { page } fixture automatically creates a fresh browser context and page for each test.

Manual Context Management

Context Lifecycle

From client/browser.ts:58-90:
Each context is:
  1. Created fresh - No shared state
  2. Configured independently - Own options
  3. Tracked separately - Independent lifecycle
  4. Closed cleanly - Resources released

State Management

Cookies

Cookies are isolated per context:

Storage

localStorage and sessionStorage are isolated:

Network State

Each context has independent network state:

Sharing State (When Needed)

Storage State

Sometimes you want to share authentication state:
Even when sharing auth state, contexts remain isolated for other state (new cookies, localStorage changes, etc.).

Persistent Context

For development/debugging, use persistent context:
Don’t use persistent context for tests. It breaks isolation.

Resource Cleanup

Automatic Cleanup

Playwright Test automatically cleans up:

Manual Cleanup

Async Disposal

From client/browser.ts:172-174:

Parallel Testing

Isolation enables parallel test execution:
With configuration:

Common Pitfalls

Sharing Browser Context

Global State

Test Order Dependencies

Best Practices

Always create a fresh context for each test.
Always close contexts and browsers.
Don’t share state between tests.
Each test should set up its own prerequisites.

Testing with Playwright Test

Playwright Test provides automatic isolation:
Each test gets:
  • Fresh browser (or shared based on config)
  • Fresh context
  • Fresh page
  • Automatic cleanup

Isolation Verification

Verify your tests are isolated:

Next Steps

Best Practices

Testing best practices

Parallelization

Running tests in parallel

Test Runner

Playwright Test API