Engineering Tutorials

A practical test pyramid with Playwright

The test pyramid is an old idea that still holds: many fast tests at the bottom, a few slow ones at the top. The hard part is deciding where a given check belongs. This guide works through that decision, using Playwright for the upper layers.

By BuildQore · · 3 min read

The three layers

Each layer trades speed for realism. Unit tests run in milliseconds but prove little about how the system fits together. End-to-end tests exercise the real thing but are slower and have more reasons to fail. A healthy suite uses the cheapest layer that can catch a given bug.

  1. L1 End-to-endPlaywrightReal browserStagingA small set of critical journeys: sign in, checkout, submit a form.
  2. L2 Integration and APIPlaywright requestHTTP contractsTest databaseServices and data working together, without a browser.
  3. L3 UnitVitest or JestPure functionsComponentsBusiness rules and edge cases, fast enough to run on every save.
From the top: fewer tests, more realism, slower feedback.

Decide by asking what could break

Start from the failure you want to catch, not from the feature.

  • A price is calculated wrongly: that is logic. Write a unit test and cover twenty input combinations in milliseconds.
  • The API accepts an invalid order: that is a contract. Write an API test.
  • A cookie banner covers the submit button: only a real browser will notice. Write an end-to-end test.

A rule of thumb helps with the grey areas: push each check as low as it can go while it can still fail for the right reason. When a bug escapes, ask which layer should have caught it, and add the test there rather than at the top by default.

API tests without a browser

Playwright's request fixture sends HTTP calls directly, using the baseURL from your configuration. These tests are quick and cover validation and error handling far more cheaply than driving a form.

tests/api/orders.spec.ts
import { test, expect } from '@playwright/test';

test('rejects an order with no items', async ({ request }) => {
  const res = await request.post('/api/orders', {
    data: { customerId: 'c_123', items: [] },
  });

  expect(res.status()).toBe(422);
  expect(await res.json()).toMatchObject({ error: 'items_required' });
});

One end-to-end test that earns its place

Locate elements the way people do, by role and accessible name. These locators survive CSS refactors and double as a basic accessibility check, because a control with no name cannot be found.

tests/e2e/contact.spec.ts
import { test, expect } from '@playwright/test';

test('visitor can send an enquiry', async ({ page }) => {
  await page.goto('/contact');

  await page.getByLabel('Full name').fill('Test Visitor');
  await page.getByLabel('Email').fill('visitor@example.com');
  await page.getByLabel('Message').fill('We need a new booking flow.');
  await page.getByRole('button', { name: 'Send enquiry' }).click();

  // Web-first assertion: retries until it passes or times out.
  await expect(page.getByRole('status')).toContainText('Thank you');
});

Notice what this test does not do. It does not check every validation message or field combination, because lower layers cover those faster. It proves that a visitor can complete the journey in a real browser, with the real front end talking to a real backend.

Keeping the suite reliable

  • Let Playwright wait. Web-first assertions such as toBeVisible retry automatically, so fixed sleeps are rarely needed.
  • Give each test its own data. Tests that share an account fail in ways that depend on run order.
  • Stub third parties with page.route, so an outage in a payment sandbox does not fail your build.
  • Set trace to on-first-retry in the use section of your configuration, so a CI failure can be replayed step by step.
  • Treat a flaky test as a defect. Fix it or remove it, because a suite people ignore protects nobody.

Keep reading

All articles

Get new articles by email.

Occasional notes on building and testing software. No filler.

Used only for these updates. See the privacy policy.

Want this done on your project?

Tell us what you are building or fixing, and we will talk through how we could help.