Skip to main content

A plugin to use docker databases for pytests

Project description

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 name of the Dockerfile within the directory set as the db-build-context

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

  • db-docker-context

    • The directory to use as the docker build context.

  • db-docker-env-vars

    • A comma separated list of environment variables to pass to docker run
      • –db-docker-env-vars=FOO=BAR,PASSWORD=BAZ

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

pytest CLI using a custom image and passing environment variables to it:

$ pytest --db-dockerfile=Dockerfile --db-name=test-postgres --db-port=5432 --db-host-port=5434 --db-docker-env-vars=POSTGRES_PASSWORD=FOO,POSTGRES_USER=BAR

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-docker-db-1.1.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

pytest_docker_db-1.1.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest-docker-db-1.1.0.tar.gz.

File metadata

  • Download URL: pytest-docker-db-1.1.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for pytest-docker-db-1.1.0.tar.gz
Algorithm Hash digest
SHA256 edb8a4b829880ad2ec08e097746e6f070335b92fbe28ca20ad972830c2a64309
MD5 eba8e3ebd2208cfe2f8e8d9c9e43f20a
BLAKE2b-256 178edf96305b8aa0e54651e428d2beb4912a6fd10a4cbe79616524facc459920

See more details on using hashes here.

File details

Details for the file pytest_docker_db-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_docker_db-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for pytest_docker_db-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67b6e95929c5b5128cc9f36b1abb22c2910ea1cd522b2964349232392c31d6d5
MD5 62b9cbc206ab1eca940fbe0bb8094643
BLAKE2b-256 91a7ccfa5dbbce6e18b32f12306030031213b3b6d1e7e2fcc4c488f1c8240772

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page