# testcontainers — card for AI agents

Python library for throwaway Docker container instances in tests.

Category: Testing · PyPI package `testcontainers` · latest version 4.15.0 (checked 2026-11-03)
Install: `pip install testcontainers`
License: Apache-2.0 (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Agent Skills for it
- testcontainers-integration-tests from Aaronontheweb/dotnet-skills: `npx skills add Aaronontheweb/dotnet-skills --skill testcontainers-integration-tests`

## Numbers (AgentGid data)
- Gid Score: 47.3 (#2291 of all tools, 60 of 122 in its category)
- Downloads last week: 9,779,247 (as of 2026-11-24), +15.6% over 4 weeks
- GitHub stars: 2,322 (as of 2026-11-24)
- Last push: 2026-10-09

## Often used with
pytest 100.0%, NumPy 83.3%, OpenAI Python Library 83.3%, Pydantic 83.3%, python-dotenv 83.3%, Requests 83.3%

## Links
- AgentGid page: https://agentgid.com/tools/testcontainers-pypi/
- Homepage: https://github.com/testcontainers/testcontainers-python
- Repository: https://github.com/testcontainers/testcontainers-python


## Testcontainers for Python: real databases in your tests

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.

### Where to use 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: Run your tests on the next Postgres version by changing one image name, before you upgrade the real server. (idea, not from the official pages)
- Check database setup scripts: Run your table-creation and data-change scripts on a throwaway database to see they finish without errors. (idea, not from the official pages)

### How to start
- Make sure Docker (or another Docker-compatible runtime) is installed and running.
- Install the library with the module you need and your own database driver.

```
pip install testcontainers[postgres] sqlalchemy psycopg2
```

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

```
pytest test_redis.py
```


### License
Free for any use, also commercial, under the Apache 2.0 license. If you pass the library's code on, include its license. The images it starts, such as Postgres, have their own licenses.

### Good to know
- It does nothing unless Docker, or a program compatible with it, runs on the machine that runs the tests.
- Database drivers are not included; install the one your code uses, for example psycopg or SQLAlchemy.
- Modules for single services may change in minor versions, so pin the exact version.
- Inside a CI container it needs a Docker client and access to the Docker socket.
- The package needs Python 3.10 or newer, although one docs page still says 3.9.

Checked 2026-11-10 against: https://github.com/testcontainers/testcontainers-python/blob/main/README.md, https://github.com/testcontainers/testcontainers-python/blob/main/LICENSE.txt, https://github.com/testcontainers/testcontainers-python/blob/main/CHANGELOG.md, https://github.com/testcontainers/testcontainers-python/blob/main/docs/quickstart.md, https://github.com/testcontainers/testcontainers-python/blob/main/docs/index.md, https://github.com/testcontainers/testcontainers-python/blob/main/docs/features/garbage_collector.md, https://github.com/testcontainers/testcontainers-python/blob/main/docs/modules/postgres.md, https://github.com/testcontainers/testcontainers-python/blob/main/docs/modules/postgres_example.py, https://github.com/testcontainers/testcontainers-python/blob/main/docs/system_requirements/index.md, https://github.com/testcontainers/testcontainers-python/blob/main/docs/system_requirements/docker.md, https://github.com/testcontainers/testcontainers-python/blob/main/docs/features/wait_strategies.md, https://testcontainers-python.readthedocs.io/en/latest/, https://testcontainers.com/, https://testcontainers.com/getting-started/, https://testcontainers.com/guides/getting-started-with-testcontainers-for-python/, https://pypi.org/pypi/testcontainers/json

### Mistakes to avoid
- Don't: from testcontainers.postgres import PostgresContainer (as in the README and older guides) → Do: 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".)
- Don't: wait_for_logs(container, "ready to accept connections") → Do: 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.)
- Don't: pip install testcontainers[postgres] and expecting SQLAlchemy or psycopg to come with it → Do: pip install testcontainers[postgres] sqlalchemy psycopg2 (or the driver your code uses) (The extra no longer pulls in database drivers.)
- Don't: Running the tests inside a CI job container with no Docker access → Do: 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.)
- Don't: testcontainers>=4 in requirements → Do: 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.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
