• Stars
    star
    1,542
  • Rank 30,345 (Top 0.6 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 7 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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.

testcontainers-python

https://readthedocs.org/projects/testcontainers-python/badge/?version=latest

testcontainers-python facilitates the use of Docker containers for functional and integration testing. The collection of packages currently supports the following features.

.. toctree::

    core/README
    arangodb/README
    azurite/README
    clickhouse/README
    compose/README
    elasticsearch/README
    google/README
    kafka/README
    keycloak/README
    localstack/README
    minio/README
    mongodb/README
    mssql/README
    mysql/README
    neo4j/README
    nginx/README
    opensearch/README
    oracle/README
    postgres/README
    rabbitmq/README
    redis/README
    selenium/README

Getting Started

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

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

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.

Installation

The suite of testcontainers packages is available on PyPI, and individual packages can be installed using pip. We recommend installing the package you need by running pip install testcontainers-<feature>, e.g., pip install testcontainers-postgres.

Note

For backwards compatibility, packages can also be installed by specifying extras, e.g., pip install testcontainers[postgres].

Docker in Docker (DinD)

When trying to launch a testcontainer from within a Docker container, e.g., in continuous integration testing, two things have to be provided:

  1. The container has to provide a docker client installation. Either use an image that has docker pre-installed (e.g. the official docker images) or install the client from within the Dockerfile specification.
  2. 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.

Development and Contributing

We recommend you use a virtual environment for development (python>=3.7 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/[your python version].txt
pytest -s

Package Structure

Testcontainers is a collection of implicit namespace packages to decouple the development of different extensions, e.g., testcontainers-mysql and testcontainers-postgres for MySQL and PostgreSQL database containers, respectively. The folder structure is as follows.

# One folder per feature.
[feature name]
    # Folder without __init__.py for implicit namespace packages.
    testcontainers
        # Implementation as namespace package with __init__.py.
        [feature name]
            __init__.py
            # Other files for this
            ...
    # Tests for the feature.
    tests
        test_[feature_name].py
        ...
    # README for this feature.
    README.rst
    # Setup script for this feature.
    setup.py

Contributing a New Feature

You want to contribute a new feature or container? Great! You can do that in six steps as outlined here <https://github.com/testcontainers/testcontainers-python/blob/main/.github/PULL_REQUEST_TEMPLATE/new_container.md>__.

More Repositories

1

testcontainers-java

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.
Java
7,956
star
2

testcontainers-dotnet

A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions.
C#
3,774
star
3

testcontainers-go

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.
Go
3,503
star
4

testcontainers-node

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.
TypeScript
1,849
star
5

testcontainers-rs

A library for integration-testing against docker containers from within Rust.
Rust
697
star
6

testcontainers-scala

Docker containers for testing in scala
Scala
629
star
7

moby-ryuk

Schedule Moby/Docker containers cleanup after specific delay.
Go
182
star
8

testcontainers-dotnet-legacy

A .net fork of testcontainers - in early development
C#
165
star
9

testcontainers-clj

Control Docker containers from your test lifecycle for Clojure integration tests.
Clojure
144
star
10

testcontainers-ruby

Testcontainers for Ruby
Ruby
119
star
11

testcontainers-java-examples

Archived: This repo has been combined into the main testcontainers-java repository
Java
102
star
12

workshop

Java
100
star
13

testcontainers-elixir

Testcontainers is an Elixir library that supports ExUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
Elixir
95
star
14

testcontainers-php

https://www.testcontainers.org implementation for PHP
PHP
81
star
15

testcontainers-rs-modules-community

Community maintained modules for Testcontainers for Rust
Rust
68
star
16

testcontainers-hs

Docker containers for your integration tests! http://hackage.haskell.org/package/testcontainers
Haskell
57
star
17

testcontainers-spock

Spock extension for using Docker containers in Spock tests. (DEPRECATED, moved to main repo!)
53
star
18

testcontainers-java-spring-boot-quickstart

Testcontainers QuickStarts
Java
50
star
19

testcontainers-jooq-codegen-maven-plugin

jOOQ code generator using Testcontainers
Java
49
star
20

java-local-development-workshop

Java local development workshop
Java
47
star
21

dind-drone-plugin

Plugin for Drone CI v0.8+ to enable use of Testcontainers using Docker-in-Docker
Shell
31
star
22

testcontainers-native

Testcontainers for C/C++/Swift and other native projects, built on the top of Testcontainers for Go
Go
20
star
23

workshop-go

Go
18
star
24

community-module-registry

Testcontainers Community Module Registry
15
star
25

testcontainers-js

A javascript fork of testcontainers - in early development
TypeScript
9
star
26

vnc-recorder

Docker image that can be used to create a sidekick container for recording videos of VNC sessions hosted in other containers
Dockerfile
9
star
27

java-module-workshop

Workshop about making your own Testcontainers module and learning important things
Java
9
star
28

tc-guide-testing-spring-boot-rest-api

Getting started with Testcontainers in a Java SpringBoot Project
Java
8
star
29

tc-guide-testing-spring-boot-kafka-listener

Testing Spring Boot Kafka Listener using Testcontainers Guide
Java
8
star
30

helloworld

A Docker image to support Testcontainers' self-test suites
Go
7
star
31

testcontainers-java-repro

A template repository intended to help create isolated repro examples for Testcontainers issues
Java
7
star
32

testcontainers-showcase

Testcontainers ShowCase
Java
7
star
33

sshd-docker

Docker image with SSH daemon installed
Dockerfile
7
star
34

testcontainers-java-module-oracle-xe

Deprecated repository - moved to https://github.com/testcontainers/testcontainers-java
6
star
35

testcontainers-java-module-localstack

Deprecated repository - moved to https://github.com/testcontainers/testcontainers-java
6
star
36

tc-guide-getting-started-with-testcontainers-for-nodejs

JavaScript
6
star
37

tc-guide-testing-aws-service-integrations-using-localstack

Testing AWS Service Integrations using LocalStack guide
Java
6
star
38

testcontainers-java-module-mssqlserver

Deprecated repository - moved to https://github.com/testcontainers/testcontainers-java
5
star
39

tc-guide-simple-local-development-with-testcontainers-desktop

Simple local development with Testcontainers Desktop guide
Java
5
star
40

tc-guide-getting-started-with-testcontainers-for-dotnet

C#
5
star
41

tc-guide-securing-spring-boot-microservice-using-keycloak-and-testcontainers

Guide for Securing Spring Boot Microservice using Keycloak and Testcontainers
Java
5
star
42

tc-guide-testing-aspnet-core

Getting started with Testcontainers in an ASP.NET Core web app
C#
5
star
43

tc-guide-getting-started-with-testcontainers-for-java

Getting started with Testcontainers for Java guide
Java
4
star
44

testcontainers-java-module-mariadb

Deprecated repository - moved to https://github.com/testcontainers/testcontainers-java
4
star
45

testcontainers-groovy-script

Java
4
star
46

tc-guide-testcontainers-lifecycle

Testcontainers LifeCycle Guide
Java
3
star
47

testcontainers-nocode

3
star
48

tc-guide-testing-rest-api-integrations-using-mockserver

Testing REST API integrations using MockServer Guide
Java
3
star
49

tc-guide-getting-started-with-testcontainers-for-go

Getting started with Testcontainers for Go guide
Go
3
star
50

tc-guide-replace-h2-with-real-database-for-testing

Testing relational database repositories using Testcontainers
Java
2
star
51

tc-guide-testing-rest-api-integrations-using-wiremock

Testing REST API integrations using WireMock Guide
Java
2
star
52

testcontainers-java-module-vault

Deprecated repository - moved to https://github.com/testcontainers/testcontainers-java
2
star
53

tc-guide-working-with-jooq-flyway-using-testcontainers

Working with jOOQ and Flyway using Testcontainers Guide
Java
2
star
54

tc-guide-getting-started-with-testcontainers-for-python

Getting started with Testcontainers for Java guide
Python
2
star
55

tc-guide-testing-micronaut-kafka-listener

Testing Micronaut Kafka Listener using Testcontainers
Java
1
star
56

testcontainers-java-module-dynalite

Deprecated repository - moved to https://github.com/testcontainers/testcontainers-java
1
star
57

tc-guide-testcontainers-in-quarkus-applications

Getting started with Testcontainers in a Quarkus Project
Java
1
star
58

tc-guide-testing-rest-api-integrations-in-micronaut-apps-using-wiremock

Testing REST API Integrations in Micronaut Applications using WireMock
Java
1
star
59

testcontainers-go-fiber

Sample application of testcontainers-go and Go Fiber
Go
1
star
60

tc-guide-template

Template for a Testcontainers Guide
1
star
61

tc-guide-configuration-of-services-running-in-container

Code for Configuring application dependencies Guide
Java
1
star