-
Notifications
You must be signed in to change notification settings - Fork 0
TestcontainersPython
title: testcontainers-python radar_quadrant: Languages & Frameworks radar_ring: Assess radar_position: inner created: 2026-05-22 last_updated: 2026-05-22 related: ["LambdaUnitTesting", "DataContractsScraping", "Floci"]
testcontainers-python is the Python port of the Testcontainers library. It starts real Docker containers as part of pytest setup and tears them down automatically after the test run, enabling integration tests against real services rather than mocks or in-memory fakes.
from testcontainers.postgres import PostgresContainer
def test_user_insert():
with PostgresContainer("postgres:16") as pg:
engine = create_engine(pg.get_connection_url())
# test against a real Postgres instanceEach container is a real running service. Testcontainers manages lifecycle, port mapping, and cleanup automatically.
Postgres, MySQL, Redis, Kafka, RabbitMQ, MongoDB, Elasticsearch, LocalStack (AWS), and arbitrary containers via DockerContainer.
- Lambda Unit Testing with Moto (Assess center): Moto mocks AWS API responses in Python; Testcontainers runs the real service. The two cover adjacent layers — Moto for isolated unit tests, Testcontainers for integration tests against real databases and services.
- Floci (Assess inner): Testcontainers with the LocalStack module is an alternative local AWS testing approach to Floci.
- Data Contracts for Scraping Pipelines (Assess inner): Testcontainers enables validating Pydantic-validated scraped data against a real database schema in CI.
testcontainers-python sits at Languages & Frameworks → Assess inner. Running real services in tests catches schema migration issues, constraint violations, and query planner behaviour that mocks and in-memory fakes cannot reproduce. Inner position reflects straightforward pytest integration (pip install testcontainers), Docker as the only prerequisite, and direct applicability to any Python project with database or service dependencies. The remaining gate before Trial is use in production CI on at least one active project with verified test reliability across runs.