Skip to content

Instantly share code, notes, and snippets.

@nikoly
Last active August 6, 2017 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikoly/50d0776be85a1ed84c341a9dfe3af895 to your computer and use it in GitHub Desktop.
Save nikoly/50d0776be85a1ed84c341a9dfe3af895 to your computer and use it in GitHub Desktop.
Consumer contract test with Pact.
import atexit
import unittest
import requests
from pact import Consumer, Provider
pact = Consumer('Translator').has_pact_with(Provider('Translate Service'), pact_dir='./pacts')
pact.start_service()
atexit.register(pact.stop_service)
class TranslateServiceContract(unittest.TestCase):
mock_host="http://localhost:1234"
def _request_helper(self, path):
url = self.mock_host + path
return requests.get(url)
def test_get_translation_existing(self):
path = '/translate/1'
expected = {"en": "One", "ua": "Один"}
(pact
.given('translation for number one')
.upon_receiving('a request to get translation for number one')
.with_request('get', path)
.will_respond_with(200, body=expected))
with pact:
import pdb; pdb.set_trace()
result = self._request_helper(path).json()
self.assertEqual(result["en"], expected["en"])
def test_get_translation_not_existing(self):
path = '/translate/-1'
expected = 404
(pact
.given('for number -1 doesn\'t exist')
.upon_receiving('a request to get translation for number minus one')
.with_request('get', path)
.will_respond_with(expected, body=''))
with pact:
result = self._request_helper(path)
self.assertEqual(result.status_code, expected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment