Giter Site home page Giter Site logo

vikahl / testcontainers-python Goto Github PK

View Code? Open in Web Editor NEW

This project forked from testcontainers/testcontainers-python

0.0 0.0 0.0 735 KB

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.

Home Page: https://testcontainers-python.readthedocs.io/en/latest/

License: Apache License 2.0

Python 98.16% Makefile 1.56% Dockerfile 0.28%

testcontainers-python's Introduction

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>__.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.