Integration tests with a real database: Docker + pytest
12 steps · about 5 h 32 min · Intermediate
- The Practical Test Pyramid (article, Martin Fowler / Ham Vocke, 40 min)
Start with the idea: what an integration test is, why a database test belongs in that layer, and how it differs from a unit test.
- Software Testing Guide (article, Martin Fowler, 20 min)
This clears up the confusing terms (narrow vs broad integration tests) before you start writing them.
- What is a container? (guide, Docker Docs, 12 min)
Containers and images are the mechanism that gives each test run a disposable database, so learn them first.
- Run your first container (guide, Docker Docs, 20 min)
Hands-on practice with docker run, ps and logs, and with what happens to data when a container is removed.
- Overview of the Docker workshop (tutorial, Docker Docs, 45 min)
It covers Docker Compose with a database container, which shows the manual setup that Testcontainers later automates.
- How to use fixtures (doc, pytest docs, 40 min)
Fixtures, yield teardown, scopes and conftest.py are the pytest tools that control the container's lifecycle.
- Getting started with Testcontainers for Python (tutorial, Testcontainers, 30 min)
The official walkthrough builds your first pytest suite against a real Postgres container, with a module-scoped container and a clean table before each test.
- Test-Driven Development with Python, Testcontainers, and pytest (article, Collabnix, 25 min)
A second, test-first project that reinforces the same pattern with finalizers and a customers table.
- Testcontainers: PostgreSQL, Redis, Kafka Testing (article, Botmonster, 25 min)
It adds Alembic migrations in the fixture and per-test isolation using transaction rollback with savepoints.
- Testcontainers Python: pytest + Docker Integration Testing (2026) (article, HelpMeTest, 25 min)
It compares fixture scopes (session vs function) and autouse cleanup, so you can trade speed against isolation.
- Testcontainers Python Pytest — Integration Testing Guide 2026 (article, QASkills, 35 min)
It goes further with SQLAlchemy sessions, async drivers, container reuse and CI configuration.
- Container Testing with Testcontainers - pytest-test-categories (article, pytest-test-categories docs, 15 min)
Last, it covers marking container tests as slower tests and skipping them cleanly when Docker isn't available, for a robust suite.