Skip to main content

CI

A helper for working with Hoverfly from pytest. Works both locally and in CI.

Installation

pip install pytest-hoverfly

or

poetry add pytest-hoverfly --dev

Usage

There are two use cases: to record a new test and to use recordings.

Prerequisites

You need to have Docker installed. pytest-hoverfly uses it under the hood to create Hoverfly instances.

Create a directory to store simulation files. Pass --hoverfly-simulation-path option when calling pytest. The path may be absolute or relative to your pytest.ini file. E.g. if you have a structure like this:

├── myproject
    ├── ...
├── pytest.ini
└── tests
    ├── conftest.py
    ├── simulations

Then put this in you pytest.ini:

[pytest]
addopts =
    --hoverfly-simulation-path=tests/simulations

Without Docker Desktop

If you're using something like lima instead of Docker Desktop, you need to specify a path to Docker API. For lima:

export DOCKER_HOST=unix:///Users/<YOUR-USER>/.lima/default/sock/docker.sock

If you're using 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:

export SERVICE_HOST=192.168.0.xxx

How to record a test

from pytest_hoverfly import hoverfly
import requests


@hoverfly('my-simulation-file', record=True)
def test_google_with_hoverfly():
    assert requests.get('https://google.com').status_code == 200

Write a test. Decorate it with @hoverfly, specifying a name of a file to save the simulation to. Run the test. A Hoverfly container will be created, and HTTP_PROXY and HTTPS_PROXY env vars will be set to point to this container. After test finishes, the resulting simulation will be exported from Hoverfly and saved to a file you specified. After test session ends, Hoverfly container will be destroyed (unless --hoverfly-reuse-container is passed to pytest).

This will work for cases when a server always returns the same response for the same request. If you need to work with stateful endpoints (e.g. wait for Teamcity build to finish), use @hoverfly('my-simulation, record=True, stateful=True). See Hoverfly docs for details.

How to use recordings

Remove record parameter. That's it. When you run the test, it will create a container with Hoverfly, upload your simulation into it, and use it instead of a real service.

from pytest_hoverfly import hoverfly
import requests


@hoverfly('my-simulation-file')
def test_google_with_hoverfly():
    assert requests.get('https://google.com').status_code == 200

Caveat: if you're using an HTTP library other than aiohttp or requests you need to tell it to use Hoverfly as HTTP(S) proxy and to trust Hoverfly's certificate. See _patch_env fixture for details on how it's done for aiohttp and requests.

How to re-record a test

Add record=True again, and run the test. The simulation file will be overwritten.

Change Hoverfly version

To use a different Hoverfly version, specify --hoverfly-image. It must be a valid Docker image tag.

Start Hoverfly with custom parameters

Use --hoverfly-args. It is passed as is to a Hoverfly container.

Usage in CI

CI 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.

For Github Actions:

services:
  hoverfly:
    image: spectolabs/hoverfly:v1.3.2
    ports:
      - 8500:8500
      - 8888:8888

  env:
    HOVERFLY_HOST: localhost
    HOVERFLY_PROXY_PORT: 8500
    HOVERFLY_ADMIN_PORT: 8888

Mind that all three variables must be specified.

Release files for pytest-hoverfly 5.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-hoverfly 5.0.4
File Size Uploaded
pytest_hoverfly-5.0.4.tar.gz 11.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-hoverfly 5.0.4
File Interpreter ABI Platform
pytest_hoverfly-5.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 22.4 kB

Release files / pytest_hoverfly-5.0.4.tar.gz

Download URL pytest_hoverfly-5.0.4.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d0b6479f4bad8f3c700a83fdb5230e7e4125c49e62a8e6c7ee85c83c5d55372a
BLAKE2b-256 checksum
How to use checksums
a6927647f1095f4466ac2646555cf460b9bdcab1f56649a89bd5e42aaba7c57f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.3 CPython/3.7.15 Linux/5.15.0-1031-azure

Release files / pytest_hoverfly-5.0.4-py3-none-any.whl

Download URL pytest_hoverfly-5.0.4-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
39379d31bc92ed3cbfc77ab191e52ba3627115fb32500f4470a12356ce136614
BLAKE2b-256 checksum
How to use checksums
02247fa27140aa78689e330d02a261fcf5e8bcc543cd12054f02bd1b54769c7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.3 CPython/3.7.15 Linux/5.15.0-1031-azure

Release history Release notifications | RSS feed

This release

5.0.4 This release

2 release files

5.0.3

2 release files

5.0.2

2 release files

5.0.1

2 release files

5.0.0

2 release files

4.0.3

2 release files

4.0.2

2 release files

4.0.1

2 release files

4.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page