Skip to content

Testing strategy

Introduction

Write code that checks code automatically: after a change, run it and know within minutes whether you broke anything that used to work.

Why use it

  • Dare to change code: tests back you up, so refactors and dependency upgrades are less scary.
  • Catch early: run automatically in CI so problems are blocked before merge.
  • Serve as documentation: tests show how the code is expected to be used.

The testing pyramid

Shaped like a pyramid: many at the bottom, few at the top.

Layer What it tests Speed Count
Unit tests A single function or class; no database or network Milliseconds Most
Integration tests Several parts together, such as an API + a real database Seconds Moderate
End-to-end (E2E) tests Full flows like a user, from the UI or entry point Slow and often flaky Fewest; cover only core flows

Turning it upside down (lots of E2E, almost no unit tests) is the “ice-cream cone”—slow and brittle—and many teams abandon testing because of it.

How to do it

  1. Unit-test business rules: price calculations and permission checks are most worth testing and easiest to test.
  2. Integration-test anything that talks to the outside: SQL, caches, message queues. Do not fake them with an in-memory database—SQL dialect differences let tests pass while production fails.
  3. Use Testcontainers for real dependencies: it starts a real database container with Docker for the test and tears it down afterward, keeping environments clean and consistent.
  4. Keep only a few critical E2E flows, such as “register → order → pay”.
  5. Make tests independent of each other: no reliance on run order; each test prepares its own data.
  6. Wire into CI; failures must block merges.

Component comparison

Unit tests

Self-hosted open sourceOpen sourceOpsLow

Small tests for a single function or class that touch no database or network, verifying "is this logic calculating correctly".

Best forThe business rules and pure logic of every project; the foundation of testing.

Avoid whenYou want to verify that SQL, caches, and APIs work together, which needs integration tests.

Pros

Run in milliseconds, so you know immediately whether a one-line change broke anything

A failure pinpoints exactly which logic is wrong

Easiest to write, and the best value for covering business rules (pricing, permission checks)

Cons

They do not touch databases and APIs, so passing does not mean the whole system works

Overusing mock objects ties tests too tightly to the implementation, and a refactor turns everything red

Testing trivial getter/setter code has no value and easily turns into chasing coverage numbers

Integration tests (Testcontainers)

Self-hosted open sourceOpen sourceOpsMedium

During tests, automatically start real databases and other dependencies with Docker and destroy them afterward, verifying the code works with real dependencies.

Best forAPI-style projects that need to verify database queries, migrations, caches, and message queues working together.

Avoid whenDocker cannot be used in the dev or CI environment, or the project has no external dependencies and unit tests suffice.

Pros

A real database is used, so dialect and constraint problems show up early

Every run starts clean and is not polluted by the previous run's data

You no longer keep a dedicated test database by hand

Cons

Both the machine and CI need Docker, and pulling images makes the tests slower

You manage the container lifecycle and clearing data, which has a learning cost

A container occasionally fails to start, and that shows more on a resource-tight CI

Official docs ↗

End-to-end tests (Playwright)

Self-hosted open sourceOpen sourceOpsMedium

Automatically opens a real browser and clicks, types, and pages like a user, verifying the whole flow from UI to backend.

Best forWebsites or admin backends with pages, covering only a few key flows such as "register - log in - place order".

Avoid whenPure backend API projects, or when you want it to cover every detailed branch (leave that to unit and integration tests).

Pros

Waits for elements automatically, so it fails randomly from timing issues less than older tools

Closest to real user behavior, and finds frontend-backend integration problems

Supports Chromium, Firefox, and WebKit browsers, with recording, screenshots, and debugging tools

Cons

Much slower than other tests and costly to maintain, since a UI change may require test changes

Can still fail intermittently, and takes time to stabilize

Needs a complete runtime environment and test data, and browsers must be installed in CI

Official docs ↗

Contract tests (Pact)

Self-hosted open sourceOpen sourceOpsMedium

Consumer and provider first agree on a contract of "what the interface looks like", and each side verifies on its own that it complies, without starting both services together for integration.

Best forSystems with multiple microservices or separate frontend and backend teams where interfaces change often.

Avoid whenMonoliths and small teams, where integration tests are enough.

Pros

When services are maintained by different teams, catches before release whether an interface change would break the other side

No need to build a full integration environment, so faster than end-to-end tests

The contract itself is a clear interface description

Cons

Needs cooperation between teams, with both sides writing and maintaining contract tests, so the process cost is not low

Only verifies interface format and conventions, not whether business logic is correct

Little benefit for monoliths or when one person maintains both frontend and backend

Official docs ↗

Checklist

  • Core business rules have unit tests
  • External dependencies such as databases and caches use real components in integration tests (for example Testcontainers)
  • E2E covers only a few critical flows
  • Tests are independent and can run in any order
  • Tests run automatically in CI and failures block merges
  • Flaky tests are fixed or quarantined promptly
  • Every bug fix adds a test that reproduces it