Library provides lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container
Project description
Python port for testcontainers-java that allows using docker containers for functional and integration testing. Testcontainers-python provides capabilities to spin up docker containers (such as a database, Selenium web browser, or any other container) for testing.
Currently available features:
Selenium Grid containers
Selenium Standalone containers
MySql Db container
MariaDb container
Neo4j container
OracleDb container
PostgreSQL Db container
Microsoft SQL Server container
Generic docker containers
LocalStack
Installation
The testcontainers package is available from PyPI, and it can be installed using pip
. Depending on which containers are needed, you can specify additional dependencies as extras:
# Install without extras
pip install testcontainers
# Install with one or more extras
pip install testcontainers[mysql]
pip install testcontainers[mysql,oracle]
Basic usage
import sqlalchemy
from testcontainers.mysql import MySqlContainer
with MySqlContainer('mysql:5.7.17') as mysql:
engine = sqlalchemy.create_engine(mysql.get_connection_url())
version, = engine.execute("select version()").fetchone()
print(version) # 5.7.17
The snippet above will spin up a MySql 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.
More extensive documentation can be found at Read The Docs.
Usage within Docker (i.e. in a CI)
When trying to launch a testcontainer from within a Docker container two things have to be provided:
The container has to provide a docker client installation. Either use an image that has docker pre-installed (e.g. the [official docker images](https://hub.docker.com/_/docker)) or install the client from within the Dockerfile specification.
The container has to have access to the docker daemon which can be achieved by mounting /var/run/docker.sock or setting the DOCKER_HOST environment variable as part of your docker run command.
Setting up a development environment
We recommend you use a virtual environment for development. Note that a python version >=3.6
is required. After setting up your virtual environment, you can install all dependencies and test the installation by running the following snippet.
pip install -r requirements/$(python -c 'import sys; print("%d.%d" % sys.version_info[:2])').txt
pytest -s
Adding requirements
We use pip-tools
to resolve and manage dependencies. If you need to add a dependency to testcontainers or one of the extras, run pip install pip-tools
followed by make requirements
to update the requirements files.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for testcontainers-3.4.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7626f2899f869b929c14b8eb4996b1e70a4b4bd1934de500be193db89b18f7cc |
|
MD5 | 9c76d652c750d3f51243ab487cebc6ab |
|
BLAKE2b-256 | a9f54e6a1a557eb64a12ddbd373000ce2ec51e2a1a5dd6633d181831984ac255 |