Principle:Microsoft Playwright Set Up Test Resources
| Knowledge Sources | |
|---|---|
| Domains | API_Testing, HTTP, Test_Setup |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Creating test fixtures and preconditions by making API calls to set up required server-side state before tests run ensures that each test begins with a known, predictable environment.
Description
Effective API testing requires that the system under test is in a known state before assertions are made. Rather than relying on pre-existing data that may drift or be modified by other tests, the setup phase programmatically creates all necessary resources through API calls. This approach is commonly referred to as test fixture creation or test data seeding.
The setup phase typically involves:
- Creating user accounts: Registering test users with known credentials so that authentication tests have valid accounts to work with.
- Populating data: Creating entities such as products, orders, or articles that subsequent tests will read, update, or delete.
- Configuring permissions: Setting up roles, access controls, or feature flags that tests depend on.
- Establishing relationships: Creating linked resources (e.g., a user with an associated profile, an order with line items) that tests need to exercise join or lookup operations.
The critical characteristic of API-based setup is that it bypasses the UI layer entirely. This makes setup faster, more reliable, and less brittle than UI-driven setup. If the UI changes, the API-based setup remains stable as long as the API contract is maintained.
Usage
Apply this principle whenever:
- Tests require specific server-side state that does not exist by default.
- You need deterministic, reproducible test conditions across environments.
- UI-based setup would be slow, fragile, or impractical.
- Multiple tests in a suite share common preconditions (e.g., an authenticated user, a populated catalog).
- You are following the Arrange-Act-Assert test pattern and need to implement the "Arrange" phase.
Theoretical Basis
Test resource setup follows the Arrange-Act-Assert (AAA) pattern, where the "Arrange" phase prepares the system:
// ARRANGE: Set up test preconditions via API
beforeAll(async ({ request }) => {
// Create a test user
await request.post("/api/users", {
data: { username: "testuser", password: "securepass", role: "admin" }
})
// Create test data that tests will operate on
await request.post("/api/articles", {
data: { title: "Test Article", body: "Content for testing" }
})
})
// ACT & ASSERT: Tests execute against the prepared state
test("can retrieve article", async ({ request }) => {
response = await request.get("/api/articles")
assert(response.ok())
})
The beforeAll hook pattern ensures setup runs once before the entire test group, rather than before each individual test. This is more efficient when the setup is expensive and the tests do not modify the shared state, or when each test creates its own isolated modifications.
Key design considerations:
- Idempotency: Setup operations should be safe to run multiple times without causing errors (e.g., using "create if not exists" semantics).
- Independence: Each test suite's setup should not depend on or interfere with another suite's setup.
- Scope: Setup can be scoped to the entire test file (
beforeAll), individual tests (beforeEach), or a project-level global setup.