============================================
Docker containers as test fixtures made easy
============================================
.. image:: https://badge.fury.io/py/dockerfixtures.svg
:alt: Latest version on Pypi: ?
:target: https://badge.fury.io/py/dockerfixtures
.. image:: https://img.shields.io/pypi/pyversions/dockerfixtures.svg
:alt: Supported Python versions: ?
:target: https://pypi.org/project/dockerfixtures
.. image:: https://travis-ci.com/cans/python-docker-fixtures.svg?branch=master
:alt: Build status (travis.com): ?
:target: https://travis-ci.com/cans/python-docker-fixtures
.. image:: https://circleci.com/gh/cans/python-docker-fixtures.svg?style=svg
:alt: Build status (circleci.com): ?
:target: https://circleci.com/gh/cans/python-docker-fixtures
.. image:: https://codecov.io/gh/cans/python-docker-fixtures/branch/master/graph/badge.svg
:alt: Test coverage: ? %
:target: https://codecov.io/gh/cans/python-docker-fixtures
This package was inspired by others, but after trying to make heads and tails
of them when faced with bugs, I ended-up finding simpler to start over.
Using dockerfixtures with pytest
================================
To spawn a container in your tests, proceed as follow:
.. code-block:: Python
import docker
from dockerfixtures import image, container
import pytest
@pytest.fixture(scope='session')
def docker_client():
return docker.from_env()
@pytest.fixture(scope='session')
def pg_image() -> image.Image:
return image.Image('postgres', tags='12')
@pytest.fixture(scope='function')
def pg_container(docker_client: docker.client.DockerClient,
pg_image: image.Image) -> container.Container:
yield from container.fixture(docker_client, some_image)
# If you don't need to reuse the image
@pytest.fixture(scope='session')
def pg_container(docker_client) -> container.Container:
some_image = image.Image('postgres', tags='12')
yield from container.fixture(docker_client, some_image)
Why not a pytest plugin ?
=========================
Other implementation of this have been provinding a pytest
plugin, so you might wonder why this one doesn't ?
First reason is I have not looked into it that much, yet.
But anyhow, you would still need to import the
``dockerfixtures.image`` module. So I am not very sure what the
benefits would be ?
Also I found those plugins to provide somewhat bizarre API, for
example to define the fixtures' scope. I haven't looked into
why they do that, yet. Here there are no surprises, a container
fixture looks like any other fixture.
Pytest plugins are global: they have to be imported in your
`top-level`_ ``conftest.py`` (see note). I think it is good
practice to keep your tests properly partitioned based on their
external dependencies. It can help split workload if the need
arises. In a collaborative environment, having to import
``dockerfixtures``, may help prevent breaking that partitioning
during reviews.
.. _top-level: https://docs.pytest.org/en/latest/writing_plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file
Raw data
{
"_id": null,
"home_page": "https://github.com/cans/python-docker-fixtures",
"name": "dockerfixtures",
"maintainer": "",
"docs_url": null,
"requires_python": "<3.12,>=3.7",
"maintainer_email": "",
"keywords": "docker,testing,fixtures",
"author": "Nicolas CANIART",
"author_email": "nicolas@caniart.net",
"download_url": "",
"platform": "any",
"description": "============================================\nDocker containers as test fixtures made easy\n============================================\n\n.. image:: https://badge.fury.io/py/dockerfixtures.svg\n :alt: Latest version on Pypi: ?\n :target: https://badge.fury.io/py/dockerfixtures\n.. image:: https://img.shields.io/pypi/pyversions/dockerfixtures.svg\n :alt: Supported Python versions: ?\n :target: https://pypi.org/project/dockerfixtures\n.. image:: https://travis-ci.com/cans/python-docker-fixtures.svg?branch=master\n :alt: Build status (travis.com): ?\n :target: https://travis-ci.com/cans/python-docker-fixtures\n.. image:: https://circleci.com/gh/cans/python-docker-fixtures.svg?style=svg\n :alt: Build status (circleci.com): ?\n :target: https://circleci.com/gh/cans/python-docker-fixtures\n.. image:: https://codecov.io/gh/cans/python-docker-fixtures/branch/master/graph/badge.svg\n :alt: Test coverage: ? %\n :target: https://codecov.io/gh/cans/python-docker-fixtures\n\n\nThis package was inspired by others, but after trying to make heads and tails\nof them when faced with bugs, I ended-up finding simpler to start over.\n\nUsing dockerfixtures with pytest\n================================\n\nTo spawn a container in your tests, proceed as follow:\n\n.. code-block:: Python\n\n import docker\n from dockerfixtures import image, container\n import pytest\n\n @pytest.fixture(scope='session')\n def docker_client():\n return docker.from_env()\n\n @pytest.fixture(scope='session')\n def pg_image() -> image.Image:\n return image.Image('postgres', tags='12')\n\n @pytest.fixture(scope='function')\n def pg_container(docker_client: docker.client.DockerClient,\n pg_image: image.Image) -> container.Container:\n yield from container.fixture(docker_client, some_image)\n\n # If you don't need to reuse the image\n\n @pytest.fixture(scope='session')\n def pg_container(docker_client) -> container.Container:\n some_image = image.Image('postgres', tags='12')\n yield from container.fixture(docker_client, some_image)\n\n\nWhy not a pytest plugin ?\n=========================\n\nOther implementation of this have been provinding a pytest\nplugin, so you might wonder why this one doesn't ?\n\nFirst reason is I have not looked into it that much, yet.\n\nBut anyhow, you would still need to import the\n``dockerfixtures.image`` module. So I am not very sure what the\nbenefits would be ?\n\nAlso I found those plugins to provide somewhat bizarre API, for\nexample to define the fixtures' scope. I haven't looked into\nwhy they do that, yet. Here there are no surprises, a container\nfixture looks like any other fixture.\n\nPytest plugins are global: they have to be imported in your\n`top-level`_ ``conftest.py`` (see note). I think it is good\npractice to keep your tests properly partitioned based on their\nexternal dependencies. It can help split workload if the need\narises. In a collaborative environment, having to import\n``dockerfixtures``, may help prevent breaking that partitioning\nduring reviews.\n\n\n.. _top-level: https://docs.pytest.org/en/latest/writing_plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file\n",
"bugtrack_url": null,
"license": "GPLv2",
"summary": "Cointainers as Pytest fixtures made easy",
"version": "0.2.4",
"split_keywords": [
"docker",
"testing",
"fixtures"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "adf7c4cebccf441c63386809d58abbf9d9371546f6542b0711e7cc5bad4c1e7f",
"md5": "4d44b5a65771829ac28886a010557e6d",
"sha256": "12b5328fa4da384766127456e64053a108e197f250102d5dfe6e72ae584a7e48"
},
"downloads": -1,
"filename": "dockerfixtures-0.2.4-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "4d44b5a65771829ac28886a010557e6d",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": "<3.12,>=3.7",
"size": 19588,
"upload_time": "2023-01-14T22:45:15",
"upload_time_iso_8601": "2023-01-14T22:45:15.648626Z",
"url": "https://files.pythonhosted.org/packages/ad/f7/c4cebccf441c63386809d58abbf9d9371546f6542b0711e7cc5bad4c1e7f/dockerfixtures-0.2.4-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-14 22:45:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "cans",
"github_project": "python-docker-fixtures",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"circle": true,
"lcname": "dockerfixtures"
}