app | ||
tests | ||
.gitignore | ||
install.sh | ||
Pipfile | ||
README.md | ||
rundemo.sh |
API TESTING WITH HOVERFLY (HoverPy)
WHAT
This is a very simple project to demonstrate the perported uses of HoverFly (an api mocking/simulation tool). In this case, the testing use case is demonstrated by way of a custom pytest suite found in the tests directory.
HOW
1> Run the included install.sh
script. This will:
- confirm that you have python3 installed and ready for use
- confirm that you have the HoverFly CLI installed and ready for use
- confirm that you have pipenv for python3, and configure your project virtual environment
2> Run the included rundemo.sh
script, after install. This will:
- use the pipenv environment to execute the hoverfly tests with pytest. What are these tests doing? 1: fire up the flask app. 2: hit both the GET endpoint and the POST endpoint, and execute a simple assert on the response JSONs. The test is actually using the stored session data for its comparison, rather than testing within the session itself. The suite deletes the session data files, when all tests are complete.
DETAILS
The /app
directory contains a very simple Flask server app, designed to emulate a live application with available api endpoints. It's useless beyond the scope of this demo. It models two simple GET requests, and a simple POST request, both of which return json responses as many of our service APIs do.
The /tests
directory contains a few standard Flask app unit tests, merely for reference. The main event, is the test_hov.py
file, which contains the test code that includes the use of HoverFly capture/simulate code. It's actually a bit underwhelming, because of how easy it is to use in tests.
The point of having both sets of tests, is to show the difference between what a set of unit tests on an api project would look like, compared with a separate set of hoverfly api tests, which would be written to talk directly to a live api (note how the hoverfly tests require me to start the server in the setup).
I'm not sure if the hoverfly CLI binaries are actually necessary for the python bindings to work, but I added the installation of them here anyway, just to be sure, and to provide some documentation for future installations.
Why does it matter that we're using stored session data, rather than executing asserts during an active session? Well, for one thing, the stored session data could be used to execute mocked tests at a later date (the server need not be live). Or, mock sessions could be used to model CONTRACTS. Or, they could be used for ongoing development on a new endpoint.
More experimentation needs to be done, to determine the full value of the tool.
INSTALLATION EXAMPLE:
[13:48:24][~/Repositories/gitlab/gregory/pyfly-demo]
gregory@orpheus $ ./install.sh (28m) |[master]
PREPARING PYTHON3 ENVIRONMENT...
PREPARING HOVERFLY INSTALLATION...
PREPARING PROJECT VIRTUAL ENVIRONMENT...
Python 3.7.3
pipenv==2018.11.26
Dependency Graph:
Flask==1.0.3
- click [required: >=5.1, installed: 7.0]
- itsdangerous [required: >=0.24, installed: 1.1.0]
- Jinja2 [required: >=2.10, installed: 2.10.1]
- MarkupSafe [required: >=0.23, installed: 1.1.1]
- Werkzeug [required: >=0.14, installed: 0.15.4]
hoverpy==0.2.2
- requests [required: Any, installed: 2.22.0]
- certifi [required: >=2017.4.17, installed: 2019.3.9]
- chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
- idna [required: >=2.5,<2.9, installed: 2.8]
- urllib3 [required: >=1.21.1,<1.26,!=1.25.1,!=1.25.0, installed: 1.25.2]
pytest==4.5.0
- atomicwrites [required: >=1.0, installed: 1.3.0]
- attrs [required: >=17.4.0, installed: 19.1.0]
- more-itertools [required: >=4.0.0, installed: 7.0.0]
- pluggy [required: >=0.9,<1.0,!=0.10, installed: 0.11.0]
- py [required: >=1.5.0, installed: 1.8.0]
- setuptools [required: Any, installed: 41.0.1]
- six [required: >=1.10.0, installed: 1.12.0]
- wcwidth [required: Any, installed: 0.1.7]
ALL SYSTEMS GO!
EXECUTION EXAMPLE:
[13:22:37][~/Repositories/gitlab/gregory/pyfly-demo]
gregory@orpheus $ ./rundemo.sh (2m) |[master]
========================================================================= test session starts ==========================================================================
platform darwin -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- /Users/gregory/.virtualenvs/pyfly-demo-Cl6C4Sqb/bin/python
cachedir: .pytest_cache
rootdir: /Users/gregory/Repositories/gitlab/gregory/pyfly-demo
collected 4 items
tests/test_hov.py::TestUsingHoverpy::test_version_call PASSED [ 25%]
tests/test_hov.py::TestUsingHoverpy::test_random_numbers PASSED [ 50%]
tests/test_hov.py::TestUsingHoverpy::test_random_string PASSED [ 75%]
tests/test_hov.py::TestUsingHoverpy::test_hashname PASSED [100%]
======================================================================= 4 passed in 0.62 seconds =======================================================================