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.
📊 A healthy test-count ratio (the pyramid)
Unit testsFast and cheap
70 tests
Integration tests
20 tests
End-to-end testsSlow and expensive
10 tests
Illustrative data for magnitude/trend, not measured results, and not a hard ratio.
How to do it
Unit-test business rules: price calculations and permission checks are most worth testing and easiest to test.
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.
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.
Keep only a few critical E2E flows, such as “register → order → pay”.
Make tests independent of each other: no reliance on run order; each test prepares its own data.
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
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