Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 1.0.1 instead.
Reason given by maintainers: Broken :(

Pytest Hoverfly Wrapper

This pytest plugin allows easy integration of Hoverfly into your tests. Hoverfly is a proxy server that can intercept requests and return custom responses. More info on Hoverfly: https://hoverfly.io/

Installation

Clone the repository and then install using setup.py:

python setup.py install

This will also automatically install the plugin's dependencies. Alternatively, install via pip:

pip install pytest-hoverfly-wrapper

Testing

The quickest way is to run tox:

pip install tox
tox -e <py35/py36/etc>

You can also run in pytest to make use of its debugging tools: (Assumes you have a virtual environment set up for a compatible Python version - see setup.py for compatible versions)

python setup.py install
pip install -r requirements-test.txt
pytest tests/

end_to_end tests the plugin's integration with a Pytest framework: each test consists of a pytest script that gets fed into pytester and run inside a virtual Pytest environment. The result of that is asserted against. The reason we need this is to test failure modes, which would cause the test to fail.

unit contains tests for individual functions.

Usage example

Cache responses to external services

Adding the setup_hoverfly fixture will stand up a Hoverfly server instance running on port 8500. You can then use this as a proxy that saves the responses to any requests make via the proxy. If the test passes, the saved responses will be dumped to file, which will be used when the test runs again.

# Enable the fixture explicitly in your tests or conftest.py (not required when using setuptools entry points)
from pytest_hoverfly_wrapper import GeneratedSimulation
import requests
import pytest

@pytest.mark.simulated(GeneratedSimulation(file="some_file.json"))
def test_something(setup_hoverfly):
    proxy_port = setup_hoverfly[1]
    proxies = {
     "http": "http://localhost:{}".format(proxy_port),
     "https": "http://localhost:{}".format(proxy_port),
    }
    requests.get("https://urlwedontwanttospam.com", proxies=proxies)
    

After running the test for the first time, you will find a file located at ./test_data/generated/some_file.json, containing all the requests made using the proxy, as well as the responses to them. Upon running the test the second time, the test will load the file and attempt to match requests to the list in the file. If a successful match is found, the matching response will be served. If not, the request will be made to its original target and the target's response will be served instead.

Completely fake responses

You can also specify your own custom responses.

# Enable the fixture explicitly in your tests or conftest.py (not required when using setuptools entry points)
from pytest_hoverfly_wrapper import StaticSimulation
import requests
import pytest

@pytest.mark.simulated(StaticSimulation(files=["google_returns_404.json"]))
def test_something(setup_hoverfly):
    proxy_port = setup_hoverfly[1]
    proxies = {
     "http": "http://localhost:{}".format(proxy_port),
     "https": "http://localhost:{}".format(proxy_port),
    }
    r = requests.get("http://google.com", proxies=proxies)
    assert r.status_code == 404

Full code is in sample/

Hoverfly crashes

Occasionally, the Hoverfly proxy might crash mid-test. If this happens, the test will raise HoverflyCrashException, which gives you clarity of why the test failed and can be caught in your testing framework as part of some test retrying logic.

Logging

pytest-hoverfly-wrapper uses the in-built logging module for logs. To import the logger:

import logging
from pytest_hoverfly_wrapper import LOGGER_NAME
hoverfly_logger = logging.getLogger(LOGGER_NAME)

Then customise the logger as necessary.

Debugging

In all scenarios, when a response is sent by Hoverfly rather than a remote server, that response will have the Hoverfly-Cache-Served header set. This differentiates the two types of response, and helps debug situations where you think a response is being served by Hoverfly but isn't, e.g. when Hoverfly fails to match the request even though you're expecting it to.

At the end of the test, the plugin will create a network.json file containing the list of all requests made (and responses received) during the test, including parameters and headers.

Release History

  • 0.1.0
    • Initial release
  • 0.1.1
    • Updates the description in the PyPi page.
  • 0.1.2
    • Create test data directory if it doesn't exist
  • 0.1.3
    • Put the bugfix in 0.1.2 in its correct place and remove extraneous plugin.py code
  • 0.1.4
    • Fixes broken domain blocking functionality
  • 0.2.0
    • Bug fixes and command line option to pass custom parameters to Hoverfly executable command
  • 0.3.0
    • Expose Journal API for accessing journal
  • 0.3.2
    • Fixes bug where block_domains is ignored if a simulation file isn't specified in StaticSimulation
  • 0.3.3
    • Registers simulated marker used by plugin
  • 0.4.0
    • Strips Expires property from Set-Cookie headers in recorded simulations
  • 0.4.1
    • Fixes typo in installation instructions
  • 0.5.0
    • Records simulations for static simulations if they don't exist yet
  • 0.5.1
    • Clean up of code styling, docs and tests.
  • 1.0.0
    • Automatic download of binaries; support for <3.7 dropped; support added for Windows

Meta

For all queries contact Veli Akiner: https://www.linkedin.com/in/veli-akiner-70a19b69/

Distributed under a modified MIT license. See LICENSE for more information.

https://github.com/kopernio/pytest-hoverfly-wrapper

Contributing

  1. Fork it (https://github.com/yourname/yourproject/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-hoverfly-wrapper-1.0.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_hoverfly_wrapper-1.0.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest-hoverfly-wrapper-1.0.0.tar.gz.

File metadata

  • Download URL: pytest-hoverfly-wrapper-1.0.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.16

File hashes

Hashes for pytest-hoverfly-wrapper-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4202631797efb1cc0bd0b3458c4e04a7d856aad5bf6a4f00a0a7028a7c08908f
MD5 980c9ffc61bb959fbe196a944359993d
BLAKE2b-256 b2999be3e09bd87788d9897b4b67fb21d72fc63063f1601785c87a8443e9de25

See more details on using hashes here.

File details

Details for the file pytest_hoverfly_wrapper-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_hoverfly_wrapper-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39afa97d580ef4b2bf7283c4ffbc4e7119772b802321e519c5321de5d8061240
MD5 f95b0f84f0dde235c6f11172ea935fa9
BLAKE2b-256 a23453343d8ec84ea7146a4d31eb4434060957cfc1e6ef27956cb7d041180c07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page