Real Tests: Why We Never Fake the Database

ForkTex Engineering · November 27, 2026 · 2 min

An integration test checks code together with the real systems it talks to: the database, the cache, the file storage. Ours start those systems in throwaway containers for every test run, and never replace them with fakes.

testcontainers, an open-source library, starts PostgreSQL, Redis or MinIO in Docker, on a pinned version, when the test suite begins, and stops it when the suite ends.

The problem with fakes

A fake database returns what its author expected. Every bug worth catching at that layer lives exactly where the expectation is wrong: a transaction that does not roll back as assumed, an error with a different shape, a timestamp column that refuses a value without a time zone. The test against the fake passes, and production fails.

A test that substitutes the thing it is meant to exercise proves only that the substitute behaves as written.

What we fake, and what we never do

Fake thisNever thisBecause
One of your own functionsThe database sessionThe first isolates a collaborator; the second removes the thing under test
An outbound HTTP callThe database driverA call across the network is a boundary; the driver is what the container exists to prove
A third-party service's SDKOur shared infrastructure libraryThe vendor's behaviour is not what the test checks; ours is

Fast enough to run every time

One container starts per test run, not per test. Each test runs inside a transaction that is rolled back at its end, so tests never see each other's data and the container's start-up is paid once.

@pytest.fixture(scope="session")
def postgres_url() -> Iterator[URL]:
    container, url = start_postgres()      # one container per test run
    try:
        yield url
    finally:
        container.stop()


@pytest_asyncio.fixture
async def session(postgres_url: URL) -> AsyncIterator[AsyncSession]:
    engine = create_async_engine(postgres_url)
    async with engine.connect() as conn:
        tx = await conn.begin()
        async with AsyncSession(bind=conn) as s:
            yield s
        await tx.rollback()                # one transaction per test, rolled back

What it costs

The suite needs Docker. A machine without it may skip these tests, loudly and counted; continuous integration always has it. A run takes a few seconds longer than one against fakes, and in exchange a passing test means the query, the constraint and the column type were really checked.

Every one of our repositories tests this way, through the test atom of its delivery standard, and each has a coverage floor that is only ever raised.