Testcontainers
A family of libraries that start real dependencies such as databases in throwaway containers for the length of a test.
In more detail
It exists for Python, Java, Go, C#, JavaScript and other languages. It starts a pinned image, waits for it to be ready, exposes the mapped port to your test and removes the container afterwards.
Example
@Container static PostgreSQLContainer<?> database = new PostgreSQLContainer<>("postgres:16");Related terms
- integration test — A test that checks several real parts working together, such as the service talking to a real database.
- PostgreSQL — A widely used open source relational database server with strict typing, rich data types and extensions.
- container — A running program that has been fenced off from the rest of the computer so it only sees its own files, network and processes.
- port publishing — Forwarding a port on the host to a port inside a container so the service can be reached from outside.
- database migration — A scripted, versioned change to a database's structure, such as adding a table or a constraint.
- health check — A regular automated test of whether a program or service is working, such as a request to its health endpoint or a command run inside its container.
- fixture — A named piece of setup, defined with @pytest.fixture, that a test receives by listing the fixture's name as a parameter.
Taught in
- Green Tests, Red Production — Confluent Apache Kafka Certifications (CCDAK and CCAAK) Exam Prep
- Spring Data Repositories — Modern Java: Virtual Threads & High-Throughput Services
- Testing APIs With MockMvc — Modern Java: Virtual Threads & High-Throughput Services
- Database Integration With Testcontainers — Modern Java: Virtual Threads & High-Throughput Services
- Integration Testing With Containers — Automated Testing, TDD & Quality Engineering
References
- Testcontainers for Java
- Testcontainers: Postgres module
- Testcontainers: JUnit 5 integration
- Spring Boot: Testcontainers
- Testcontainers
- testcontainers-python documentation