Skip to content

Commit f501f19

Browse files
Merge pull request #161 from pact-foundation/docs/verifier_docs_examples
docs: Improve examples for verify
2 parents 9c01e99 + 9875c71 commit f501f19

27 files changed

+35
-411
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,3 @@ ENV/
102102

103103
.noseids
104104

105-
106-
# ignore local test pact files
107-
**/userserviceclient-userservice.json
108-

Makefile

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ define E2E
3535
cd examples/e2e
3636
pip install -r requirements.txt
3737
pip install -e ../../
38-
pytest tests/test_user_consumer.py
38+
pytest
3939
./verify_pact.sh
4040
endef
4141
export E2E
@@ -44,21 +44,8 @@ export E2E
4444
e2e:
4545
bash -c "$$E2E"
4646

47-
define verifier
48-
echo "verifier make"
49-
cd examples/verifier
50-
pip install -r requirements.txt
51-
pip install -e ../../
52-
pytest
53-
endef
54-
export verifier
55-
56-
.PHONY: verifier
57-
verifier:
58-
bash -c "$$verifier"
59-
6047
.PHONY: examples
61-
examples: verifier e2e
48+
examples: e2e
6249

6350

6451

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,11 @@ For more information see [Matching](https://docs.pact.io/getting_started/matchin
293293
## Verifying Pacts Against a Service
294294

295295
In addition to writing Pacts for Python consumers, you can also verify those Pacts
296-
against a provider of any language. After installing pact-python a `pact-verifier`
296+
against a provider of any language. There are two ways to do this.
297+
298+
### CLI
299+
300+
After installing pact-python a `pact-verifier`
297301
application should be available. To get details about its use you can call it with the
298302
help argument:
299303

@@ -357,6 +361,18 @@ as the environment variable `PACT_BROKER_PASSWORD`.
357361
The bearer token to use when contacting the Pact Broker. You can also specify this value
358362
as the environment variable `PACT_BROKER_TOKEN`.
359363

364+
### Python API
365+
You can use the Verifier class. This has all the same parameters as the cli tool but allows you to write native python code and the test framework of your choice.
366+
367+
```python
368+
verifier = Verifier(provider='UserService',
369+
provider_base_url=PACT_URL)
370+
371+
output, logs = verifier.verify_pacts('./userserviceclient-userservice.json')
372+
373+
```
374+
You can see more details in the [e2e examples](https://github.com/pact-foundation/pact-python/tree/master/examples/e2e/tests/provider/test_provider.py).
375+
360376
### Provider States
361377
In many cases, your contracts will need very specific data to exist on the provider
362378
to pass successfully. If you are fetching a user profile, that user needs to exist,
File renamed without changes.

examples/e2e/.vscode/settings.json

Lines changed: 0 additions & 8 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

examples/e2e/README.md

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
# Introduction
22

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.
3+
This is an e2e example to show the provider verification working for both cli and python api.
44

55
## Setup
66

77
Create your own virtualenv for this. Run
88

99
```bash
1010
pip install -r requirements.txt
11-
pip install pact-python
11+
pip install ../../
1212
```
1313

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
14+
pip install pipenv
15+
$ pipenv install
16+
pipenv shell
17+
pytest
1818

19-
Then from where this is install run in it's own terminal
19+
This should provide you with a relative path to pact install relatively (2 dirs up)
2020

21+
Create the local broker (for demo purposes only) Open a separate terminal in the examples/broker folder and run:
2122
```bash
2223
docker-compose up
2324
```
@@ -37,47 +38,3 @@ Or you can run individual tests like:
3738
```bash
3839
pytest tests/test_user_consumer.py::test_get_non_existing_user
3940
```
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.

examples/e2e/requirements.txt

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
attrs==19.3.0
2-
certifi==2019.11.28
3-
chardet==3.0.4
4-
click==7.1.1
5-
enum34==1.1.10
61
Flask==1.1.1
7-
idna==2.9
8-
importlib-metadata==1.6.0
9-
itsdangerous==1.1.0
10-
Jinja2==2.11.1
11-
MarkupSafe==1.1.1
12-
more-itertools==8.2.0
13-
packaging==20.3
14-
pluggy==0.13.1
15-
psutil==5.7.0
16-
py==1.8.1
17-
pyparsing==2.4.6
182
pytest==5.4.1
19-
requests==2.23.0
20-
six==1.14.0
21-
urllib3==1.25.8
22-
wcwidth==0.1.9
23-
Werkzeug==1.0.1
24-
zipp==3.1.0
3+
requests==2.23.0

examples/e2e/tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
12
def pytest_addoption(parser):
23
parser.addoption(
34
"--publish-pact", type=str, action="store",
45
help="Upload generated pact file to pact broker with version"
56
)
7+
8+
parser.addoption(
9+
"--provider-url", type=str, action="store",
10+
help="The url to our provider."
11+
)

0 commit comments

Comments
 (0)