{"id":20152399,"url":"https://github.com/wrike/pytest-hoverfly","last_synced_at":"2025-04-09T21:22:51.389Z","repository":{"id":42686579,"uuid":"344109146","full_name":"wrike/pytest-hoverfly","owner":"wrike","description":"Hoverfly integration for pytest","archived":false,"fork":false,"pushed_at":"2023-01-30T15:19:05.000Z","size":73,"stargazers_count":9,"open_issues_count":5,"forks_count":5,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-23T23:15:23.085Z","etag":null,"topics":["hoverfly","pytest","service-virtualization","testing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wrike.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-03T11:50:50.000Z","updated_at":"2024-04-20T19:56:38.000Z","dependencies_parsed_at":"2023-02-16T08:40:17.004Z","dependency_job_id":null,"html_url":"https://github.com/wrike/pytest-hoverfly","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wrike%2Fpytest-hoverfly","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wrike%2Fpytest-hoverfly/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wrike%2Fpytest-hoverfly/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wrike%2Fpytest-hoverfly/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wrike","download_url":"https://codeload.github.com/wrike/pytest-hoverfly/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248112924,"owners_count":21049755,"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":["hoverfly","pytest","service-virtualization","testing"],"created_at":"2024-11-13T23:11:34.325Z","updated_at":"2025-04-09T21:22:51.367Z","avatar_url":"https://github.com/wrike.png","language":"Python","readme":"[![CI](https://github.com/wrike/pytest-hoverfly/actions/workflows/main.yml/badge.svg)](https://github.com/wrike/pytest-hoverfly/actions/workflows/main.yml)\n\n\nA helper for working with [Hoverfly](https://hoverfly.readthedocs.io/en/latest/) from `pytest`. Works both locally and in CI.\n\n### Installation\n`pip install pytest-hoverfly`\n\nor\n\n`poetry add pytest-hoverfly --dev`\n\n\n### Usage\nThere are two use cases: to record a new test and to use recordings.\n\n#### Prerequisites\nYou need to have [Docker](https://www.docker.com/) installed. `pytest-hoverfly` uses it under the hood to create Hoverfly instances.\n\nCreate a directory to store simulation files. Pass `--hoverfly-simulation-path` option\nwhen calling `pytest`. The path may be absolute or relative to your `pytest.ini` file.\nE.g. if you have a structure like this:\n```\n├── myproject\n    ├── ...\n├── pytest.ini\n└── tests\n    ├── conftest.py\n    ├── simulations\n```\n\nThen put this in you pytest.ini:\n```\n[pytest]\naddopts =\n    --hoverfly-simulation-path=tests/simulations\n```\n\n#### Without Docker Desktop\nIf you're using something like [lima](https://github.com/lima-vm/lima) instead of Docker Desktop, you need to specify a path to Docker API. For lima:\n\n`export DOCKER_HOST=unix:///Users/\u003cYOUR-USER\u003e/.lima/default/sock/docker.sock`\n\nIf you're using [minikube](https://github.com/kubernetes/minikube) instead of Docker Desktop, you need to specify the service host because the exposed ports are not available on localhost. For minikube you get the service IP with `minikube ip` command and then put it in the env var:\n\n`export SERVICE_HOST=192.168.0.xxx`\n\n#### How to record a test\n```python\nfrom pytest_hoverfly import hoverfly\nimport requests\n\n\n@hoverfly('my-simulation-file', record=True)\ndef test_google_with_hoverfly():\n    assert requests.get('https://google.com').status_code == 200\n```\n\nWrite a test. Decorate it with `@hoverfly`, specifying a name of a file to save the simulation to.\nRun the test. A Hoverfly container will be created, and  `HTTP_PROXY` and `HTTPS_PROXY` env vars\nwill be set to point to this container. After test finishes, the resulting simulation will\nbe exported from Hoverfly and saved to a file you specified. After test session ends, Hoverfly\ncontainer will be destroyed (unless `--hoverfly-reuse-container` is passed to pytest).\n\nThis will work for cases when a server always returns the same response for the same\nrequest. If you need to work with stateful endpoints (e.g. wait for Teamcity build\nto finish), use `@hoverfly('my-simulation, record=True, stateful=True)`. See\n[Hoverfly docs](https://docs.hoverfly.io/en/latest/pages/tutorials/basic/capturingsequences/capturingsequences.html)\nfor details.\n\n#### How to use recordings\nRemove `record` parameter. That's it. When you run the test, it will create a container\nwith Hoverfly, upload your simulation into it, and use it instead of a real service.\n\n```python\nfrom pytest_hoverfly import hoverfly\nimport requests\n\n\n@hoverfly('my-simulation-file')\ndef test_google_with_hoverfly():\n    assert requests.get('https://google.com').status_code == 200\n```\n\nCaveat: if you're using an HTTP library other than `aiohttp` or `requests` you need to\ntell it to use Hoverfly as HTTP(S) proxy and to trust Hoverfly's certificate. See\n`_patch_env` fixture for details on how it's done for `aiohttp` and `requests`.\n\n#### How to re-record a test\nAdd `record=True` again, and run the test. The simulation file will be overwritten.\n\n\n#### Change Hoverfly version\nTo use a different Hoverfly version, specify `--hoverfly-image`. It must be a valid Docker image tag.\n\n#### Start Hoverfly with custom parameters\nUse `--hoverfly-args`. It is passed as is to a Hoverfly container.\n\n### Usage in CI\nCI systems like Gitlab CI or Github Actions allow you to run arbitrary services as containers. `pytest-hoverfly` can detect if a Hoverfly instance is already running by looking at certain environment variables. If it detects a running instance, `pytest-hovefly` uses it, and doesn't create a new container.\n\nFor Github Actions:\n\n```\nservices:\n  hoverfly:\n    image: spectolabs/hoverfly:v1.3.2\n    ports:\n      - 8500:8500\n      - 8888:8888\n\n  env:\n    HOVERFLY_HOST: localhost\n    HOVERFLY_PROXY_PORT: 8500\n    HOVERFLY_ADMIN_PORT: 8888\n```\n\nMind that all three variables must be specified.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwrike%2Fpytest-hoverfly","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwrike%2Fpytest-hoverfly","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwrike%2Fpytest-hoverfly/lists"}