Package: pytest-hoverfly
A helper for working withHoverflyfrompytest. Works both locally and in CI.Installationpip install pytest-hoverflyorpoetry add pytest-hoverfly --devUsageThere are two use cases: to record a new test and to use recordings.PrerequisitesYou need to haveDockerinstalled.pytest-hoverflyuses it under the hood to create Hoverfly instances.Create a directory to store simulation files. Pass--hoverfly-simulation-pathoption
when callingpytest. The path may be absolute or relative to yourpytest.inifile.
E.g. if you have a structure like this:├── myproject
    ├── ...
├── pytest.ini
└── tests
    ├── conftest.py
    ├── simulationsThen put this in you pytest.ini:[pytest]
addopts =
    --hoverfly-simulation-path=tests/simulationsWithout Docker DesktopIf you're using something likelimainstead 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.sockIf you're usingminikubeinstead 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 withminikube ipcommand and then put it in the env var:export SERVICE_HOST=192.168.0.xxxHow to record a testfrompytest_hoverflyimporthoverflyimportrequests@hoverfly('my-simulation-file',record=True)deftest_google_with_hoverfly():assertrequests.get('https://google.com').status_code==200Write 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, andHTTP_PROXYandHTTPS_PROXYenv 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-containeris 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). SeeHoverfly docsfor details.How to use recordingsRemoverecordparameter. 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.frompytest_hoverflyimporthoverflyimportrequests@hoverfly('my-simulation-file')deftest_google_with_hoverfly():assertrequests.get('https://google.com').status_code==200Caveat: if you're using an HTTP library other thanaiohttporrequestsyou need to
tell it to use Hoverfly as HTTP(S) proxy and to trust Hoverfly's certificate. See_patch_envfixture for details on how it's done foraiohttpandrequests.How to re-record a testAddrecord=Trueagain, and run the test. The simulation file will be overwritten.Change Hoverfly versionTo use a different Hoverfly version, specify--hoverfly-image. It must be a valid Docker image tag.Start Hoverfly with custom parametersUse--hoverfly-args. It is passed as is to a Hoverfly container.Usage in CICI systems like Gitlab CI or Github Actions allow you to run arbitrary services as containers.pytest-hoverflycan detect if a Hoverfly instance is already running by looking at certain environment variables. If it detects a running instance,pytest-hoveflyuses 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: 8888Mind that all three variables must be specified.