|
| 1 | +# Introduction |
| 2 | + |
| 3 | +This is an e2e example using flask to help as a guide to getting started with Pact for Python. For the provider it imports the app into a wrapper (pact_provider.py) so as to decorate extra functions we can use to test our app. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +Create your own virtualenv for this. Run |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install -r requirements.txt |
| 11 | +pip install pact-python |
| 12 | +``` |
| 13 | + |
| 14 | +TODO: Make this so you can point to the parent install to help with development. |
| 15 | + |
| 16 | +Create the local broker (for demo purposes only) To do this separately clone this repo: |
| 17 | +* https://github.com/pact-foundation/pact-broker-docker |
| 18 | + |
| 19 | +Then from where this is install run in it's own terminal |
| 20 | + |
| 21 | +```bash |
| 22 | +docker-compose up |
| 23 | +``` |
| 24 | + |
| 25 | +If you can open a browser to http://localhost and see the broker you have succeeded. |
| 26 | + |
| 27 | +## Consumer |
| 28 | + |
| 29 | +From the root directory run: |
| 30 | + |
| 31 | +```bash |
| 32 | +pytest |
| 33 | +``` |
| 34 | + |
| 35 | +Or you can run individual tests like: |
| 36 | + |
| 37 | +```bash |
| 38 | +pytest tests/test_user_consumer.py::test_get_non_existing_user |
| 39 | +``` |
| 40 | + |
| 41 | +If you want to publish this to the pact broker add the '--publish-pact' option like: |
| 42 | + |
| 43 | +```bash |
| 44 | +pytest --publish-pact=XX |
| 45 | +``` |
| 46 | + |
| 47 | +XX is the version number of the pact and is for you to manage in your deployment process. |
| 48 | + |
| 49 | +Sometimes you may get the mock server in a hung state. You can kill it via (untested): |
| 50 | + |
| 51 | +```bash |
| 52 | +pkill -f pact-mock-service.rb |
| 53 | +``` |
| 54 | + |
| 55 | +## Provider States |
| 56 | + |
| 57 | +Run the script (placeholder version number for pact broker) |
| 58 | + |
| 59 | +```bash |
| 60 | +./verify_pact.sh 1 |
| 61 | +``` |
| 62 | + |
| 63 | +This will import the provider.py file which is the actual app and then decorate it with extra urls. It then puts this into the background and runs the pact-verifier tool against it. |
| 64 | + |
| 65 | +To test what this looks like when failing change one of these values. |
| 66 | + |
| 67 | +```python |
| 68 | +def setup_user_a_nonadmin(): |
| 69 | + |
| 70 | + id = '00000000-0000-4000-a000-000000000000' |
| 71 | + some_date = '2016-12-15T20:16:01' |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +### Provider debugging |
| 76 | + |
| 77 | +To manually trigger one of the 2 manual states you can run: |
| 78 | + |
| 79 | +```bash |
| 80 | +curl -X POST -H "Content-Type: application/json" --data "{\"state\": \"UserA exists and is not an administrator\"}" http://127.0.0.1:5000/_pact/provider_states |
| 81 | +``` |
| 82 | + |
| 83 | +Changing the json content to match the state you want. |
0 commit comments