Field notes on a problem I would call solved, if more teams used the solution.

Integration tests need real dependencies: a database, a cache, a message broker. Teams get them in one of three ways. They point at a shared QA database. They install everything on each developer’s laptop by hand. Or they swap in a lightweight fake and hope. All three fail in predictable ways, and the failures waste more hours than the setup would have.

What goes wrong with the usual options

The shared QA database is the worst. Two pipelines run at once, both insert a customer with the same email, one fails. Someone’s manual testing leaves a half-finished order that breaks a count. Nobody can reproduce a failure because the data that caused it was overwritten an hour later. My opinion: a shared mutable database is not a test environment, it is a rumour.

Local installs drift. One developer runs PostgreSQL 13, another 15, the CI server has 14. A query works on two and fails on one, and the argument about whose machine is wrong takes the afternoon.

In-memory fakes are fast but they are a different product. An in-memory database may accept SQL that your production engine rejects, or reject features you use, such as particular JSON operators, locking behaviour or collation rules. Passing against the fake tells you the code works against the fake.

The idea

Start the real thing, in a container, from the test code itself, and throw it away afterwards. Testcontainers is the best-known library for this. It began in Java and now has versions for Python, Node.js, Go, .NET and others. It talks to a local Docker-compatible runtime, pulls the image you name, starts it, waits until the service is actually ready, hands your test the host and the mapped port, and removes the container when the run ends.

Three details matter more than they look.

  • Random ports. The container’s port is mapped to a free one on the host, so two runs on the same machine never collide. You read the port from the library; you do not hard-code 5432.
  • Readiness waits. “Container started” is not “database accepting connections”. The library lets you wait on a log line, an open port or a health check. Skipping this is the usual cause of tests that fail only on the first run.
  • Cleanup. Testcontainers runs a small helper container whose job is to remove what the test started, even when the test process is killed. Without something like that, abandoned containers pile up on build agents.

One container per test, per class, or per run?

This is the real design decision. Starting a database takes seconds, so one per test method is too slow for a large suite. One per whole run is fast but brings back shared state between tests.

A common middle path: one container per test class or per run, and reset the data between tests. Reset by rolling back a transaction, truncating tables, or restoring a template database. Pick one and stick to it. Rolling back is quickest but cannot test code that commits its own transactions; truncation is slower but honest about what the application did.

Some teams also create the schema once from migration scripts, the same ones production uses. That makes the migrations themselves tested on every run, which is a quiet bonus.

A small cost sheet

Illustrative numbers. A team in Coimbatore has 180 integration tests. Starting the database container takes about 6 seconds after the image is cached; the first pull on a fresh CI agent may take much longer, depending on image size and network. Running with one shared container and truncation between tests costs perhaps 20 milliseconds per test, so about 3.6 seconds of resets, plus the 6-second start. Under 10 seconds of overhead for a database that behaves exactly like production.

Compare the old setup: a nightly job against a shared database, with roughly one failure a week caused by someone else’s data. If each such failure takes an engineer an hour to rule out, that is about 50 hours a year of triage for a problem that no longer exists.

Where it stops working

Be honest about the limits.

  • It needs a container runtime on every machine that runs the tests. Locked-down corporate laptops and some CI setups make that hard. Check before you promise it to the team.
  • Images for commercial databases may carry licence terms. Read them.
  • A container of the same engine and version is close to production, not identical. Cluster behaviour, replication lag and managed-service quirks will not show up.
  • Dependencies you do not own, such as a payment gateway, cannot be containerised. Use a stub or a virtualised service there.

Pin image versions. An image tagged latest will change under you one morning and break a build for reasons nobody can see in the repository. Use the same version production runs, and update it deliberately.

My recommendation: use real dependencies in containers for the integration layer, keep fakes only for unit tests, and spend your debugging time on actual bugs.

Figures and names in the examples are illustrative.

Leave a Reply

Trending