Skip to content
Agents tracked: 506 Releases (7d): 634 Agent status: 3 with issuesSince last limit reset: Claude 15d · Codex 0d For AI agents: Markdown Updated Nov 26, 2026

testcontainers

Real databases in your tests.

TestingPYPI

GitHubWebsite

1. License

“Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license”

Apache 2.0 allows commercial use free of charge; it is usually only a test tool, not shipped to users.

github.com/testcontainers/testcontainers-python/blob/main/LICENSE.txt

From the package registry and GitHub, checked daily; card checked Nov 10, 2026. Tap a verdict to see its source.How we checkedReport an error

What testcontainers does

Testcontainers is a free Python library that starts a real database, cache or other service in Docker just for your tests, and removes it afterwards. Your tests then talk to the same kind of Postgres or Redis you run in production instead of a fake. It needs Docker running on the machine.

What people build with it

Test against a real Postgres

Your tests for sign-up or checkout run their queries on a fresh Postgres database that disappears when the tests end.

Give each test its own Redis

Tests that use a cache or a job queue each get a clean Redis on their own port, so they can run side by side.

Run the same tests in CI

Your automated checks on every push start the same services, as long as the CI machine can reach Docker.

No leftovers after a crash

If a test run is killed halfway, a small helper container still removes the services it started.

Try a database upgrade first Idea

Run your tests on the next Postgres version by changing one image name, before you upgrade the real server.

Check database setup scripts Idea

Run your table-creation and data-change scripts on a throwaway database to see they finish without errors.

testcontainers popularity

47 / 100
Gid Score, #2291 overall
9.8M
downloads a week, pypi
2,322
GitHub stars
testcontainers: downloads per weekPyPI, testcontainers 9,731,176
testcontainers: GitHub starstestcontainers/testcontainers-python 2,322

Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated

How testcontainers is used

  • testcontainers joined the catalog on its public numbers (9,779,247 downloads a week on PyPI and 2,322 GitHub stars), not on our project count: it is a dependency in 6 public GitHub projects of our scan, 0 of them set up for AI coding agents.
  • Projects that use it most often also use pytest (100.0%), NumPy (83.3%), OpenAI Python Library (83.3%).

Often used with

In 100.0% of its projects
In 83.3% of its projects
In 83.3% of its projects
In 83.3% of its projects
In 83.3% of its projects
In 83.3% of its projects
In 83.3% of its projects
In 66.7% of its projects

More about testcontainers

Agent Skills for it
  • testcontainers-integration-tests: Write integration tests using TestContainers for .NET with xUnit. Covers infrastructure testing with real databases, message queues, and cac

See all

More in Testing

Alternatives

testcontainers This page

Apache-2.0Gid 479.8M/wk

fakeredis

Pick fakeredis when Docker is not available and an in-memory fake of Redis is good enough for your Python tests.

Moto

Pick Moto when you only need to mock AWS services in Python tests, without containers.

Docker Python

Pick the Docker Python library when you want to drive the Docker Engine yourself rather than use ready test helpers.

Compare testcontainers with another tool

Get started

Tell your AI assistant

“Use testcontainers for Python (pin testcontainers==4.15.0, Python 3.10+) with imports from testcontainers.community.<module>, start containers in with blocks or pytest fixtures, install the database driver separately, and use wait strategies instead of wait_for_logs.”

  1. Make sure Docker (or another Docker-compatible runtime) is installed and running.

    From github.com/testcontainers/testcontainers-python/blob/main/docs/system_requirements/docker.md

  2. Install the library with the module you need and your own database driver.

    pip install testcontainers[postgres] sqlalchemy psycopg2

    From github.com/testcontainers/testcontainers-python/blob/main/docs/modules/postgres.md

  3. Start the service in a with block inside your test and run the tests with pytest.

    pytest test_redis.py

    From github.com/testcontainers/testcontainers-python/blob/main/docs/quickstart.md

Mistakes to avoid

from testcontainers.postgres import PostgresContainer (as in the README and older guides)
from testcontainers.community.postgres import PostgresContainer

In 4.15.0 the old paths still work but warn; our run printed "testcontainers.postgres is deprecated, use testcontainers.community.postgres instead".

wait_for_logs(container, "ready to accept connections")
container.waiting_for(LogMessageWaitStrategy("ready to accept connections")) with LogMessageWaitStrategy from testcontainers.core.wait_strategies

String and callable predicates for wait_for_logs are deprecated in 4.15.0 and modules moved to wait strategies.

pip install testcontainers[postgres] and expecting SQLAlchemy or psycopg to come with it
pip install testcontainers[postgres] sqlalchemy psycopg2 (or the driver your code uses)

The extra no longer pulls in database drivers.

Running the tests inside a CI job container with no Docker access
Use an image with the Docker client and mount /var/run/docker.sock or set DOCKER_HOST

Without a reachable Docker daemon the first container start fails; our check raised DockerException.

testcontainers>=4 in requirements
testcontainers==4.15.0, upgraded on purpose

Community modules can break in minor releases.

Questions

Does it work without Docker?

No. It needs a Docker-compatible runtime; our check without Docker failed at once with a DockerException.

Can I use Podman instead of Docker?

Yes, by setting DOCKER_HOST to the Podman socket; other runtimes are not tested as actively as Docker.

Who removes the containers if my tests crash?

A helper container called Ryuk starts with the first container and cleans up when the Python process ends or dies.

Can I start my own image, not just ready modules?

Yes. The core module runs containers from any image.