Skip to main content
See Build Status on Travis CI

A plugin to use docker databases for pytests


This Pytest plugin was generated with Cookiecutter along with @hackebrot’s Cookiecutter-pytest-plugin template.

What is this?

This is a pytest plugin that hooks into the host’s docker daemon to create and teardown containers within the pytest life cycle.

Features

  • Use custom Dockerfiles or base images from Dockerhub

  • Use volumes to persist test data in between testing sessions

  • Specify what port you want the container to be listening on the host machine and the container

  • Optionally persist the container between test sessions

  • Name the containers created

Requirements

  • docker-py>=1.10.6

  • pytest>=3.3.2

  • docker

Installation

You can install “pytest-docker-db” via pip from PyPI:

$ pip install pytest-docker-db

Configuration

  • db-volume-args

    • Provide the “-v” arguments that you would pass to the “docker run” command if you were using the cli. If you need multiple volumes mounted separate them with commas.

    • The basic syntax is /host/vol/path:/path/in/container:rw. If using a named volume, the syntax would be vol-name:/path/in/container:rw ‘For more information please visit the docker documentation

  • db-image

    • Specify the name of the image to use as the DB.

      • Must be in the form of "image_name":"tag".

  • db-name

    • Specify the name of the container. If this is not specified a random container name will be used with the prefix docker-db

  • db-host-port

    • Specify the port that the db should be listening to on the host machine.

  • db-port

    • Specify the port that the db should be listening to in the container. This is often the default port used by your database.

  • db-persist-container

    • If set, the container created will not be torn down after the test suite has ran. By default any image created will be torn down and removed after the test suite has finished.

  • db-dockerfile

    • Specify the path to the directory containing the Dockerfile to use in the build.

      • If a path is given as well as an image name, the Dockerfile will be used.

Usage

Plugin contains one fixture:

docker_db - it’s a session scoped fixture that returns a docker-py container object. For almost all use cases the user will not care about this object.

The recommended way to use this fixture is to create an autouse=True fixture in your conftest.py file to automatically invoke the setup of the containers.

@pytest.fixture(scope='session', autouse=True)
def my_docker_db(docker_db):
    pass

Can be configured via the pytest CLI or the pytest.ini file.

pytest.ini:

[pytest]
db-volume-args=/home/kp/vol:/var/lib/postgresql/data:rw
db-image=postgres:latest
db-name=test-postgres
db-port=5432
db-host-port=5434

pytest CLI using a Dockerhub image :

$ pytest --db-image=postgrest:latest --db-name=test-postgres --db-port=5432 --db-host-port=5434

pytest CLI using a custom image:

$ pytest --db-dockerfile=Dockerfile --db-name=test-postgres --db-port=5432 --db-host-port=5434

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, “pytest-docker-db” is free and open source software

Issues

If you encounter any problems, please file an issue along with a detailed description.

Release files for pytest-docker-db 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-docker-db 1.0.2
File Size Uploaded
pytest-docker-db-1.0.2.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-docker-db 1.0.2
File Interpreter ABI Platform
pytest_docker_db-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 20.3 kB

Release files / pytest-docker-db-1.0.2.tar.gz

Download URL pytest-docker-db-1.0.2.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
90f98d911f90bad47f967adf22c2f0eed59380ffaba998431e57cbda7c775139
BLAKE2b-256 checksum
How to use checksums
2518cbefbee8c242cfc52d8fc9e8fc15ab0d948975fd99f95cf6f1864655a3d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/38.4.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.3

Release files / pytest_docker_db-1.0.2-py3-none-any.whl

Download URL pytest_docker_db-1.0.2-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
91407f956d2e34d13da2509ad28c0bdf95f6a13a15a54a3444bc8725b217b640
BLAKE2b-256 checksum
How to use checksums
3b35fe86b27a5d8be549f21288edeed456b004a59c7fc377d32dff7067a71af0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/38.4.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.3
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page