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:- Independent cookies
- Separate localStorage/sessionStorage
- Isolated cache
- Independent network state
- Separate permissions
Test Isolation Pattern
Recommended Pattern
Manual Context Management
Context Lifecycle
From client/browser.ts:58-90:- Created fresh - No shared state
- Configured independently - Own options
- Tracked separately - Independent lifecycle
- 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:Resource Cleanup
Automatic Cleanup
Playwright Test automatically cleans up:Manual Cleanup
Async Disposal
Parallel Testing
Isolation enables parallel test execution:Common Pitfalls
Sharing Browser Context
Global State
Test Order Dependencies
Best Practices
One Context Per Test
One Context Per Test
Always create a fresh context for each test.
Clean Up Resources
Clean Up Resources
Always close contexts and browsers.
Avoid Global State
Avoid Global State
Don’t share state between tests.
Make Tests Self-Contained
Make Tests Self-Contained
Each test should set up its own prerequisites.
Testing with Playwright Test
Playwright Test provides automatic isolation:- 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
