Package: pytest-docker-db
A plugin to use docker databases for pytestsThisPytestplugin was generated withCookiecutteralong with@hackebrot’sCookiecutter-pytest-plugintemplate.What is this?This is a pytest plugin that hooks into the host’sdockerdaemon to create and teardown containers within the pytest
life cycle.FeaturesUse customDockerfiles or base images fromDockerhubUsevolumesto persist test data in between testing sessionsSpecify what port you want the container to be listening on the host machine and the containerOptionally persist the container between test sessionsName the containers createdRequirementsdocker-py>=1.10.6pytest>=3.3.2dockerInstallationYou can install “pytest-docker-db” viapipfromPyPI:$ pip install pytest-docker-dbConfigurationdb-volume-argsProvide 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 bevol-name:/path/in/container:rw‘For more information please visit thedocker documentation’db-imageSpecify the name of the image to use as the DB.Must be in the form of"image_name":"tag".db-nameSpecify the name of the container. If this is not specified a random container name will be
used with the prefixdocker-dbdb-host-portSpecify the port that the db should be listening to on the host machine.db-portSpecify the port that the db should be listening to in the container.
This is often the default port used by your database.db-persist-containerIf 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-dockerfileSpecify the name of the Dockerfile within the directory set as thedb-build-contextIf a path is given as well as an image name, the Dockerfile will be used.db-docker-contextThe directory to use as the docker build context.db-docker-env-varsA comma separated list of environment variables to pass todocker run–db-docker-env-vars=FOO=BAR,PASSWORD=BAZUsagePlugin contains one fixture:docker_db- it’s a session scoped fixture that returns adocker-py containerobject.
For almost all use cases the user will not care about this object.The recommended way to use this fixture is to create anautouse=Truefixture in yourconftest.pyfile
to automatically invoke the setup of the containers.@pytest.fixture(scope='session', autouse=True)
def my_docker_db(docker_db):
    passCan be configured via thepytestCLI or thepytest.inifile.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=5434pytest CLI using aDockerhubimage :$ pytest --db-image=postgrest:latest --db-name=test-postgres --db-port=5432 --db-host-port=5434pytest CLI using a custom image:$ pytest --db-dockerfile=Dockerfile --db-name=test-postgres --db-port=5432 --db-host-port=5434pytest 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=BARContributingContributions are very welcome. Tests can be run withtox, please ensure
the coverage at least stays the same before you submit a pull request.LicenseDistributed under the terms of theMITlicense, “pytest-docker-db” is free and open source softwareIssuesIf you encounter any problems, pleasefile an issuealong with a detailed description.