Skip to content

Commit db24490

Browse files
committed
Update README for broker stuff
1 parent 0741630 commit db24490

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

Diff for: README.md

+36-12
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,74 @@
11
pact-python demo
22
================
33

4-
**NOTE:** This README needs to be updated since adding examples for the pact broker
5-
6-
74
This simple client/server implementation demos how pact-python can be used for a contract test.
85

96
Further reading about pact is at https://docs.pact.io/ or https://docs.pact.io/blogs-videos-and-articles
107

118
It has 3 components:
12-
* `user-app.py` -- a simple flask app that has a REST endpoint for `/users/<name>` which returns a JSON representation of a user
13-
* `client.py` -- a simple client that gets a user from user-app.
14-
* `client-test.py` -- a set of test cases using pytest and pact-python to test a simple
9+
* `pact_python_demo/user-app.py` -- a simple flask app that has a REST endpoint for `/users/<name>` which returns a JSON representation of a user
10+
* `pact_python_demo/client.py` -- a simple client that gets a user from user-app.
11+
* `tests/test_client.py` -- a set of test cases using pytest and pact-python to test a simple
1512
contract between the client and server.
13+
* `broker/` -- contains docker-compose files for a pact broker server
1614

17-
Install this with:
15+
Set up your virtual environment with:
1816

1917
```
2018
$ pip install pipenv
2119
$ pipenv install
2220
```
2321

22+
## Creating/validating pacts without the broker
23+
2424
Enter your venv shell with:
2525
```
2626
$ pipenv shell
2727
```
2828

2929
Run the test with:
3030
```
31-
(venv) $ pytest client-test.py
31+
(venv) $ pytest
3232
```
3333

34-
You'll see a pact file is generated by this consumer test at `consumer-provider.json`
34+
You'll see a pact file is generated by this consumer test at `tests/userclientservice-userservice.json`
3535

3636
Then, fire up your server-side app and verify the provider works as expected:
3737
```
38-
(venv) $ python user-app.py
38+
(venv) $ python pact_python_demo/user-app.py
3939
(venv) $ pact-verifier --provider-base-url=http://localhost:5001 \
40-
--pact-url=./consumer-provider.json \
40+
--pact-url=tests/userclientservice-userservice.json \
4141
--provider-states-setup-url=http://localhost:5001/_pact/provider_states
4242
```
4343

44+
## Creating/validating pacts and publishing results to the pact broker
45+
Next, you can try incorporating the pact broker in this process:
46+
47+
Start the broker server:
48+
```
49+
$ cd broker
50+
$ docker-compose up
51+
```
52+
It's accessible at http://127.0.0.1 with username 'pactbroker' and password 'pactbroker'
53+
54+
55+
To run the test, this time pushing the generated pact into the broker with version '0.1' of UserServiceClient, use:
56+
```
57+
(venv) $ pytest --update-pact 0.1
58+
```
59+
60+
The `tests/conftest.py` adds this custom option, and `tests/test_client` has code in the `pact` fixture to upload to the broker if the command line option was passed.
61+
62+
63+
Then, you can validate the provider (UserService) and push the result to the broker by running `./verify_pact.sh 0.2` -- which runs the same `pact-verifier` command as above but has additional args for pulling the pact file from the broker (instead of getting the .json locally) and pushing the verification result to the broker.
64+
65+
Log in to the broker and take a look at the matrix. You will see that UserClientService version 0.1 has been verified against UserService version 0.2
66+
67+
4468
How does this work?
4569
===================
4670

47-
The purpose of `client-test.py` is to simply verify that **if the server sends me what I'm expecting, MY client code behaves properly**. It is essentially a unit test to exercise your client code using a mocked server. Except in this case, the mock server is now a "pact mock provider". In the test, you have configured the mock provider server to respond to your client with certain data. This is the mock data to represent how you'd expect the provider to behave **given the proper state exists on the provider.** Using that mock data, you verify your client-side code works.
71+
The purpose of `test_client.py` is to simply verify that **if the server sends me what I'm expecting, MY client code behaves properly**. It is essentially a unit test to exercise your client code using a mocked server. Except in this case, the mock server is now a "pact mock provider". In the test, you have configured the mock provider server to respond to your client with certain data. This is the mock data to represent how you'd expect the provider to behave **given the proper state exists on the provider.** Using that mock data, you verify your client-side code works.
4872

4973
Two tests were created to verify that my client behaves properly if: a) I get a 200OK response back w/ the json I'd expect to see, or b) I get a 404 back and I should be returning `None`
5074

0 commit comments

Comments
 (0)