Skip to content

Commit 2db7008

Browse files
committed
docs(examples): removed manaul publish to broker
1 parent d922472 commit 2db7008

File tree

3 files changed

+26
-45
lines changed

3 files changed

+26
-45
lines changed

examples/e2e/README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,17 @@ pytest
3636
Or you can run individual tests like:
3737

3838
```bash
39-
pytest tests/test_user_consumer.py::test_get_non_existing_user
39+
pytest tests/consumer/test_user_consumer.py::test_get_non_existing_user
4040
```
41+
42+
To get the consumer to publish a pact to broker you will need to run (2 is an arbitary version number):
43+
```bash
44+
pytest tests/consumer/test_user_consumer.py --publish-pact 2
45+
```
46+
47+
And then you can run:
48+
```bash
49+
./verify_pact.sh 2
50+
```
51+
52+
To verify the pact

examples/e2e/tests/consumer/test_user_consumer.py

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"""pact test for user service client"""
22

3-
import json
43
import logging
54
import os
6-
import requests
7-
from requests.auth import HTTPBasicAuth
5+
import atexit
86

97
import pytest
108
from pact import Consumer, Like, Provider, Term, Format
@@ -15,10 +13,7 @@
1513
logging.basicConfig(level=logging.INFO)
1614
print(Format().__dict__)
1715

18-
PACT_UPLOAD_URL = (
19-
"http://127.0.0.1/pacts/provider/UserService/consumer"
20-
"/UserServiceClient/version"
21-
)
16+
PACT_BROKER_URL = "http://localhost"
2217
PACT_FILE = "userserviceclient-userservice.json"
2318
PACT_BROKER_USERNAME = "pactbroker"
2419
PACT_BROKER_PASSWORD = "pactbroker"
@@ -38,42 +33,21 @@ def consumer():
3833

3934
@pytest.fixture(scope='session')
4035
def pact(request):
41-
pact = Consumer('UserServiceClient').has_pact_with(
42-
Provider('UserService'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT,
43-
pact_dir=PACT_DIR)
44-
try:
45-
print('start service')
46-
pact.start_service()
47-
yield pact
48-
finally:
49-
print('stop service')
50-
pact.stop_service()
51-
5236
version = request.config.getoption('--publish-pact')
53-
if not request.node.testsfailed and version:
54-
push_to_broker(version)
55-
56-
57-
def push_to_broker(version):
58-
"""
59-
Push to broker
60-
"""
61-
with open(os.path.join(PACT_DIR, PACT_FILE), 'rb') as pact_file:
62-
pact_file_json = json.load(pact_file)
63-
64-
basic_auth = HTTPBasicAuth(PACT_BROKER_USERNAME, PACT_BROKER_PASSWORD)
37+
publish = True if version else False
6538

66-
log.info("Uploading pact file to pact broker...")
39+
pact = Consumer('UserServiceClient', version=version).has_pact_with(
40+
Provider('UserService'), host_name=PACT_MOCK_HOST, port=PACT_MOCK_PORT,
41+
pact_dir=PACT_DIR, publish_to_broker=publish, broker_base_url=PACT_BROKER_URL,
42+
broker_username=PACT_BROKER_USERNAME, broker_password=PACT_BROKER_PASSWORD)
6743

68-
r = requests.put(
69-
"{}/{}".format(PACT_UPLOAD_URL, version),
70-
auth=basic_auth,
71-
json=pact_file_json
72-
)
73-
if not r.ok:
74-
log.error("Error uploading: %s", r.content)
75-
r.raise_for_status()
44+
print('start service')
45+
pact.start_service()
46+
atexit.register(pact.stop_service)
7647

48+
yield pact
49+
print('stop service')
50+
pact.stop_service()
7751

7852
def test_get_user_non_admin(pact, consumer):
7953
expected = {
@@ -93,14 +67,10 @@ def test_get_user_non_admin(pact, consumer):
9367
.with_request('get', '/users/UserA')
9468
.will_respond_with(200, body=Like(expected)))
9569

96-
# pact.setup()
97-
9870
with pact:
9971
user = consumer.get_user('UserA')
10072
assert user.name == 'UserA'
10173

102-
# pact.verify()
103-
10474

10575
def test_get_non_existing_user(pact, consumer):
10676
(pact

examples/e2e/tests/provider/test_provider.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import os
2121

2222
import pytest
23-
# from flask import Flask, abort, jsonify, request
2423
from multiprocessing import Process
2524

2625
from pact import Verifier

0 commit comments

Comments
 (0)