Python Apache-2.0

testcontainers-python

Testcontainers is a Python library that providing a friendly API to run Docker container. It is designed to create runtime environment to use during your automatic tests.

T

testcontainers

Dernière activité 14 sept. 2026
testcontainers/testcontainers-python

2,3 k

étoiles

389

forks

189

issues ouvertes

databasepythonpython3seleniumtestcontainerstesting

Ce README est souvent en anglais.

uv Ruff PyPI - Version PyPI - License PyPI - Python Version codecov Core Tests Community Tests Docs

Codespace

Testcontainers Python

testcontainers-python facilitates the use of Docker containers for functional and integration testing.

For more information, see the docs.

Getting Started

>>> from testcontainers.postgres import PostgresContainer
>>> import sqlalchemy

>>> with PostgresContainer("postgres:16") as postgres:
...     engine = sqlalchemy.create_engine(postgres.get_connection_url())
...     with engine.begin() as connection:
...         result = connection.execute(sqlalchemy.text("select version()"))
...         version, = result.fetchone()
>>> version
'PostgreSQL 16...'

The snippet above will spin up a postgres database in a container. The get_connection_url() convenience method returns a sqlalchemy compatible url we use to connect to the database and retrieve the database version.

Contributing / Development / Release

See CONTRIBUTING.md for more details.

Configuration

You can set environment variables to configure the library behaviour:

Env Variable Example Description
TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE /var/run/docker.sock Path to Docker's socket used by ryuk
TESTCONTAINERS_RYUK_PRIVILEGED false Run ryuk as a privileged container
TESTCONTAINERS_RYUK_DISABLED false Disable ryuk
RYUK_CONTAINER_IMAGE testcontainers/ryuk:0.8.1 Custom image for ryuk
RYUK_RECONNECTION_TIMEOUT 10s Reconnection timeout for Ryuk TCP socket before Ryuk reaps all dangling containers

Alternatively you can set the configuration during runtime:

from testcontainers.core import testcontainers_config

testcontainers_config.ryuk_docker_socket = "/home/user/docker.sock"

Projets similaires

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

Javadockerdocker-composehacktoberfest
Ttestcontainers
8,7 k étoiles1,9 k

Testcontainers is a NodeJS library that supports tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

TypeScriptdockernodetestcontainers
Ttestcontainers
2,6 k étoiles272

Testcontainers for Go is a Go package that makes it simple to create and clean up container-based dependencies for automated integration/smoke tests. The clean, easy-to-use API enables developers to programmatically define containers that should be run as part of a test and clean up those resources when the test is done.

Goautomationdockergo
Ttestcontainers
5 k étoiles641