Rest-API Contract-Testing with pact-python

adkarigar
3 min readOct 4, 2020

--

lets spinoff… Part - I

this image pretty much explains everything…

IMAGE II — [source: DZONE ]

requirements

DockerToolBox
python3.x ( pip install bottle pact-python )
consumer application = that generates the pact file (build_test.py)
provider application = that verifies the generated pact.

project-structure

pact-python-example
|_ log
|_ pact-mock-service.log
|_ pact
|_ pacts
|_ consumer-provider.json
|_ build_test.py
|_ client.py
|_ productServiceBottle.py

role of each file

1. client.py is an Consumer application which is making a request 
to external service.
2. build_test.py contains a Pact-Unit-Test of client.py,
which is making request to created mock-service, and to
publish the pact files created in pact_dir to pact-broker
( docker container ) which will be shared with provider.
3. productServiceBottle.py is a RestAPI based Provider app.
which handles GET request to Rest Endpoint under test
in client.py and POST request to provider-states-setup-url
4. pact/pacts folder contains the contract files(from step 2),
which tells the provider and consumer to adhere to.
5. log/ folder contains the log files
6. in provider-verification step,
this is playing the role of Mock-Consumer ( as seen in Image )
this will Replay and Verify , if successful,it will publish the
Verification results to broker_url , which can be seen in
pact-verification image down below.

setup pact-broker

project-structure for pact-brokerbroker-pact
|_ ssl
|_ nginx.conf
|_ nginx-selfsigned.key
|_ nginx-selfsigned.crt
|_ docker-compose.yml

execution steps

1. $ docker-compose up -d broker-pact/2. $ python -m unittest build_test.py
this will run the unittest and publish the pact to the
broker which will be running ( step 1 )
3. $ python productService.py
this service will be listening on port 3001.
this is provider URL = localhost:3001
4. run the provider verification, which is shown in the next steps

note: docker-compose contains PostgreSQL, pact-broker and nginx

$ docker ps -a
containers running from docker-compose

provider-verification

$ cd <root-project>
$ pact-verifier.exe \
--provider-base-url=http://localhost:3001 \
--pact-broker-username="pactbroker" \
--pact-broker-password="pactbroker" \
--publish-verification-results \
--provider-app-version latest \
--provider-states-setup-url=http://localhost:3001/api/_pact/state \
--pact-url=http://192.168.99.114/pacts/provider/Provider/consumer/Consumer/latest
o/p of pact-verifier
productService HTTP log for every pact-verifier command run
NOTE:1. every time you run the above pact-verifier, it calls the,
POST request /api/_pact/state in productService and then the,
GET request /builds/3455 which is in the contract file *.json

to verify the the verification results , go to HAL dashboard ( pact-broker )
running on <docker-machine ip> username/password = pactbroker/pactbroker

Pacts Verification
Last Verified column with green highlight indicates the contracts are honored from the provider side.

network graph

network graph of consumer relationships
Network graph of provider relationships

Matrix

the matrix for the provider & consumer

go to my github for full code
that’s all folks, take it easy..
GITHUB REPO

[Reference:]

https://docs.pact.io/getting_started

--

--

Responses (3)