Registry / testing / testcontainers

testcontainers

JSON →
library4.14.2pypiunverified

Testcontainers for Python is a library that provides lightweight, disposable instances of common dependencies (databases, message brokers, web browsers, etc.) for integration testing, leveraging Docker containers. The `testcontainers-core` package, while listed on PyPI, serves as an internal dependency for the main `testcontainers` library, which is the package users should install and interact with directly. The current stable version is 4.14.2, with a regular release cadence including minor and patch updates.

pip install testcontainers
INSTALL
IMPORT
SIG · TESTCONTAINERS
T
testcontainers
testingenv4.14.2
Install
4.1s avg
Import
837ms
Disk
65MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.14.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.960 runs
installs and imports cleanly · install 0.0s · import 0.879s · 65.8MB
glibc
py 3.103.960 runs
installs and imports cleanly · install 4.1s · import 0.796s · 64MB
65MB installed
● package 65MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DockerContainer
from testcontainers.core.container import DockerContainer
from testcontainers_core.container import DockerContainer
Even though 'testcontainers-core' is a package, its components are exposed through the main 'testcontainers' namespace.
wait_for_log
from testcontainers.core.waiting_it import wait_for_log
Common wait strategy for checking container logs.
MySQLContainer
from testcontainers.mysql import MySQLContainer
Specific container implementations are typically found in submodules.

This quickstart demonstrates how to use a generic `DockerContainer` with a custom command and a `wait_for_log` strategy to ensure the container is ready. The container automatically stops and cleans up upon exiting the `with` block.

from testcontainers.core.container import DockerContainer from testcontainers.core.waiting_it import wait_for_log # Start a simple Alpine container that outputs a message after a delay # using a specific version for stability with DockerContainer("alpine:3.17.3") \ .with_command("sh -c 'sleep 2 && echo \"Container is ready!\" && tail -f /dev/null'") \ .with_wait_for_service(wait_for_log("Container is ready!", timeout=10)) as alpine_container: print("Alpine container started and 'Container is ready!' message detected.") # You can access container logs for verification logs = alpine_container.get_logs() if b"Container is ready!" in logs: print("Successfully found 'Container is ready!' in logs.") else: print("Error: 'Container is ready!' not found in logs.") assert b"Container is ready!" in logs, "Expected log message not found." print("Container stopped and removed.")
Debug
Known issues
gotchaThe Docker daemon must be running for Testcontainers to function. If Docker is not available or accessible, container instantiation will fail.
fix
Ensure Docker Desktop or a Docker daemon service is running and configured correctly for your environment.
affects: All versions
deprecatedOlder decorator-based wait strategies (e.g., `@wait_for`) have been deprecated and removed from core. Using them will lead to errors.
fix
Migrate to `with_wait_for_service()` and use explicit wait strategies like `wait_for_log()`, `wait_for_http()`, `ExecWaitStrategy`, etc. (e.g., `container.with_wait_for_service(wait_for_log("message"))`).
affects: >=4.13.2
gotchaDo NOT install or use the `testcontainers-core` package directly. It is an internal dependency of the main `testcontainers` library, which is the package intended for user installation and interaction. Installing `testcontainers-core` will result in an outdated and non-functional setup.
fix
Always install `testcontainers` (e.g., `pip install testcontainers`). Imports for core components like `DockerContainer` are still made from `testcontainers.core.container`.
affects: All versions
gotchaTestcontainers relies on Docker, and running multiple or complex containers can consume significant system resources (CPU, RAM, disk I/O).
fix
Monitor your system resources during tests. Ensure containers are properly cleaned up (using `with` blocks or `stop()/remove()`) to prevent resource leaks. Consider using smaller base images where possible.
affects: All versions
gotchaUsing untagged or `latest` Docker images can lead to non-reproducible test failures, as the image content can change unexpectedly.
fix
Always pin container image versions (e.g., `mysql:8.0.36`, `alpine:3.17.3`) to ensure your tests are stable and reproducible across different environments and runs.
affects: All versions
Upgrade
Version history
4.14.2latest on PyPI · released Mar 18, 2026
Audit
Dependencies
dockerrequiredRequired for interacting with the Docker daemon.
wraptrequiredA small, cleanly self-contained module for decorators, used internally.
sqlalchemyoptionalRequired for database-specific containers (e.g., MySQLContainer, PostgreSQLContainer) if you use their `get_connection_url()` methods.
Agent activity
23 hits · last 30 days
node
20
Meta
2
OpenAI (training)
1
Resources
testcontainers — pip install testcontainers · libregistry