{"id":13501593,"url":"https://github.com/lovelysystems/lovely-pytest-docker","last_synced_at":"2025-04-06T09:09:12.984Z","repository":{"id":47678367,"uuid":"116373485","full_name":"lovelysystems/lovely-pytest-docker","owner":"lovelysystems","description":"Pytest plugin providing the ability to use docker-compose services as fixtures.","archived":false,"fork":false,"pushed_at":"2024-09-02T11:45:08.000Z","size":335,"stargazers_count":98,"open_issues_count":6,"forks_count":14,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-03-30T06:09:04.630Z","etag":null,"topics":["docker","docker-compose","fixtures","pytest","testing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lovelysystems.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-05T10:32:02.000Z","updated_at":"2025-02-10T16:46:40.000Z","dependencies_parsed_at":"2024-10-31T20:32:12.345Z","dependency_job_id":"acb852b0-876c-434a-8e04-04e48fa135df","html_url":"https://github.com/lovelysystems/lovely-pytest-docker","commit_stats":{"total_commits":41,"total_committers":6,"mean_commits":6.833333333333333,"dds":0.4390243902439024,"last_synced_commit":"21957000cb3126bac5faee57ddf4d51e41404e34"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovelysystems%2Flovely-pytest-docker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovelysystems%2Flovely-pytest-docker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovelysystems%2Flovely-pytest-docker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lovelysystems%2Flovely-pytest-docker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lovelysystems","download_url":"https://codeload.github.com/lovelysystems/lovely-pytest-docker/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247457803,"owners_count":20941906,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","docker-compose","fixtures","pytest","testing"],"created_at":"2024-07-31T22:01:42.792Z","updated_at":"2025-04-06T09:09:12.963Z","avatar_url":"https://github.com/lovelysystems.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# Lovely Pytest Docker\n\n[![PyPI](https://img.shields.io/pypi/v/lovely-pytest-docker)](https://pypi.org/project/lovely-pytest-docker/)\n[![PyPI](https://img.shields.io/pypi/pyversions/lovely-pytest-docker)](https://pypi.org/project/lovely-pytest-docker/)\n[![Build Status](https://app.travis-ci.com/lovelysystems/lovely-pytest-docker.svg?branch=master)](https://app.travis-ci.com/lovelysystems/lovely-pytest-docker)\n\nCreate simple Pytest_ fixtures for writing integration tests based on Docker\ncontainers. The framework provides a service class to start and stop containers\nbased Docker Compose. Each single container can be started individually.\n\nSome parts of this package are taken from\nhttps://github.com/AndreLouisCaron/pytest-docker\n\n## Usage with Pytest\n\nThe docker compose file should contain all desired containers and the ports\nshould be exposed. In the following example we want to start the app to test\nand a SQL database (Crate). Let's assume there is a ``Dockerfile`` for the app\nin the same folder as the docker compose file.\n\n```yaml\n\n\nversion: \"3\"\nservices:\n  app:\n    build: .\n    ports:\n      - \"8080\"\n    depends_on:\n      - \"crate\"\n\n  crate:\n    image: crate:latest\n    ports:\n      - \"4200\"\n```\n\nIn the ``conftest.py`` file we can declare the docker fixtures for each service\nwe want to be able to start in the tests.\n\n```python\n\n    import pytest\n\n    @pytest.fixture(scope='session')\n    def docker_app(docker_services):\n        docker_services.start('app')\n        public_port = docker_services.wait_for_service(\"app\", 8080)\n        url = \"http://{docker_services.docker_ip}:{public_port}\".format(**locals())\n        return url\n\n    @pytest.fixture(scope='session')\n    def docker_crate(docker_services):\n        docker_services.start('crate')\n        public_port = docker_services.wait_for_service(\"crate\", 4200)\n        dsn = \"{docker_services.docker_ip}:{public_port}\".format(**locals())\n        return dsn\n```\n\nBy default the fixture will look for the ``docker-compose.yml`` file in the\n``tests`` subfolder of the path where ``pytest.ini`` resides (or the project's\nroot directory if no ini file is given - as in the tests example). In many\ncases you will want to override the location for the docker compose files. Just\noverwrite the ``docker_compose_files`` fixture in your ``conftest.py`` file.\n\n```python\n    @pytest.fixture(scope='session')\n    def docker_compose_files(pytestconfig):\n        \"\"\"Get the docker-compose.yml absolute path.\n        Override this fixture in your tests if you need a custom location.\n        \"\"\"\n        return [\n            project_path('docker', 'docker-compose.yml'),\n        ]\n```\n\nIn your test file declare the fixtures you want to use:\n\n```python\n    def test_something(docker_app, docker_crate):\n        # e.g. initialize database\n        ...\n        # test something (e.g. request to docker_app)\n        ...\n```\n\nA working configuration and test example can be found in the ``tests`` folder\nof this package.\n\n## Execution in Docker Container\n\nIt's possible to execute a command inside one of the Docker containers. Use\nthe ``exec`` method of the ``docker_services`` fixture::\n\n```python\n    def test_execute(docker_services):\n        # the first argument is the service name of the compose file,\n        # the following arguments build the command to run\n        res = docker_services.execute('crate', 'ls', '-a')\n```\n\n## Stopping a Docker Container\n\nIt's possible to stop single Docker containers. Use\nthe ``stop`` method of the ``docker_services`` fixture::\n\n    def test_stop(docker_services):\n        # the first argument is the service name of the compose file,\n        # the following arguments build the command to run\n        res = docker_services.stop('crate')\n\n## Wait for Service\n\nThe ``wait_for_service`` method of the service module checks whether the\ndocker service is really started. By default it makes a HTTP GET request to the\nserver's ``/`` endpoint. The service will retry to check until a timeout of\n30 seconds has passed.\n\n### Custom Service Checker\n\nSome services may work differently and require a custom checker.\n\nCreate a custom service checker function which receives the IP address and the\nport as parameters::\n\n```python\n    def custom_service_checker(ip_address, port):\n        # if service is ready\n        return True\n        # otherwise return False\n```\n\nIn the fixture provide the custom service checker function as ``check_service``\nparameter to the ``wait_for_service`` method::\n\n```python\n    @pytest.fixture(scope='session')\n    def docker_custom_service(docker_services):\n        docker_services.start('custom_service')\n        public_port = docker_services.wait_for_service(\n            \"app\",\n            8080,\n            check_server=custom_service_checker\n        )\n        url = \"http://{docker_services.docker_ip}:{public_port}\".format(**locals())\n        return url\n```\n\nTo use another request path with the default checker the `url_checker` method\ncan be used to create a `check_url` method for another path::\n\n```python\n    docker_services.wait_for_service(\n        \"app\",\n        8080,\n        check_server=url_checker('/probe_status'),\n    )\n```\n\n## Run Tests\n\nTests are held in the ``tests`` directory. Running tests is done via the\n[pytest](http://doc.pytest.org) package with::\n\n```shell\n    ./gradlew pytest\n```\n\n## Publish a new Version\n\nUse gradle to build and twine to upload like this:\n\n```shell\n    ./gradlew sdist\n    twine upload build/sdist/lovely-pytest-docker-\u003cversion\u003e.tar.gz\n```\n\nNote: twine needs to be installed by othher means (e.g. ``pip install twine``)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flovelysystems%2Flovely-pytest-docker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flovelysystems%2Flovely-pytest-docker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flovelysystems%2Flovely-pytest-docker/lists"}