Skip to main content

Python implementation for Pact (http://pact.io/)

Project description

https://img.shields.io/badge/license-MIT-brightgreen.svg https://img.shields.io/badge/python-2.7,%203.3,%203.4,%203.5,%203.6-brightgreen.svg https://img.shields.io/badge/pypi-0.1.1-brightgreen.svg https://img.shields.io/pypi/wheel/Django.svg https://travis-ci.org/Kalimaha/pact-test.svg?branch=master https://coveralls.io/repos/github/Kalimaha/pact-test/badge.svg?branch=development https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg

Pact Test for Python

This repository contains a Python implementation for Pact. Pact is a specification for Consumer Driven Contracts Testing. For further information about Pact project, contracts testing, pros and cons and useful resources please refer to the Pact website.

There are two phases in Consumer Driven Contracts Testing: a Consumer sets up a contract (it’s consumer driven after all!), and a Provider honours it.

Providers Tests (Set the Contracts)

https://img.shields.io/badge/Pact-1.0-brightgreen.svg https://img.shields.io/badge/Pact-1.1-red.svg https://img.shields.io/badge/Pact-2.0-red.svg https://img.shields.io/badge/Pact-3.0-red.svg https://img.shields.io/badge/Pact-4.0-red.svg

Consumers run Provider Tests to create pacts and establish a contract between them and service providers. An example of a Python client using pact test is available at here. Consumers define all the interactions with their providers in the following way:

@service_consumer('UberEats')
@has_pact_with('Dominos Pizza')
class DominosPizzaTest(ServiceProviderTest):

    @given('some pizza exist')
    @upon_receiving('a request for an hawaiian pizza')
    @with_request({'method': 'get', 'path': '/pizzas/hawaiian/'})
    @will_respond_with({'status': 404, 'body': json.dumps({'reason': 'we do not serve pineapple with pizza'})})
    def test_get_pizza(self):
        pizza = get_pizza('hawaiian')
        assert pizza.status_code == 404

This test verifies, against a mock server, the expected interaction and creates a JSON file (the pact) that will be stored locally and also sent to the Pact Broker, if available.

Consumers Tests (Honour Your Contracts)

https://img.shields.io/badge/Pact-1.0-brightgreen.svg https://img.shields.io/badge/Pact-1.1-red.svg https://img.shields.io/badge/Pact-2.0-red.svg https://img.shields.io/badge/Pact-3.0-red.svg https://img.shields.io/badge/Pact-4.0-red.svg

Providers run Consumer Tests to verify that they are honouring their pacts with the consumers. There are few examples of an hypothetical restaurant service implemented with the most popular Python web frameworks:

There are few things required to setup and run consumer tests.

Installation

Pact Test is distributed through PyPi so it can be easily included in the requirements.txt file or normally installed with pip:

$ pip install pact-test

Pact Helper

This helper class is used by Pact Test to start and stop the web-app before and after the test. It also defines the ports and endpoint to be used by the test. The following is an example of Pact Helper:

class RestaurantPactHelper(PactHelper):
    process = None

    def setup(self):
        self.process = subprocess.Popen('gunicorn start:app -w 3 -b :8080 --log-level error', shell=True)

    def tear_down(self):
        self.process.kill()

There are few rules for the helper:

  • it must extend PactHelper class from pact_test

  • it must define a setup method

  • it must define a tear_down method

It is also possible to override default url (localhost) and port (9999):

class RestaurantPactHelper(PactHelper):
    test_url = '0.0.0.0'
    test_port = 5000

States

When a consumer sets a pact, it defines certain states. States are basically pre-requisites to your test. Before honouring the pacts, a provider needs to define such states. For example:

@honours_pact_with('UberEats')
@pact_uri('http://Kalimaha.github.io/src/pacts/pact.json')
class UberEats(ServiceConsumerTest):

    @state('some menu items exist')
    def test_get_menu_items(self):
        DB.save(MenuItem('spam'))
        DB.save(MenuItem('eggs'))

In this example, the provider stores some test data in its DB in order to make the system ready to receive mock calls from the consumer and therefore verify the pact.

Configuration

The default configuration of Pact Test assumes the following values:

  • consumer_tests_path: tests/service_consumers

  • provider_tests_path: tests/service_providers

  • pact_broker_uri: None

It is possible to override such values by creating a file named .pact.json in the project’s root. The following is an example of a valid configuration file:

{
  "consumer_tests_path": "mypath/mytests",
  "provider_tests_path": "mypath/mytests",
  "pact_broker_uri": "http://example.com/"
}

All fields are optional: only specified fields will override default configuration values.

Development

Setup

python3 setup.py install

Test

It is possible to run the tests locally with Docker through the following command:

$ ./bin/test

By default this command tests the library against Python 3.6. It is possible to specify the Python version as follows:

$ ./bin/test <ENV>

Available values for ENV are: py27, py33, py34, py35 and py36. It is also possible to test all the versions at once with:

$ ./bin/test all

Upload New Version

$ python3 setup.py sdist upload

With Python Wheels:

$ python3 setup.py sdist bdist_wheel
$ twine upload dist/*

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pact-test-0.3.94.tar.gz (15.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pact_test-0.3.94-py2.py3-none-any.whl (29.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pact-test-0.3.94.tar.gz.

File metadata

  • Download URL: pact-test-0.3.94.tar.gz
  • Upload date:
  • Size: 15.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pact-test-0.3.94.tar.gz
Algorithm Hash digest
SHA256 87dafd958645cee77607fa0fbcb6cab517174334a9b4e10ff796214e0a127566
MD5 2a3871961c3ac78506a9c96ac741d823
BLAKE2b-256 2c5ad7877a5c3b8d961bd18d7aec7d15218e5ec88151609f1d1ed7229f01a9ee

See more details on using hashes here.

File details

Details for the file pact_test-0.3.94-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pact_test-0.3.94-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cfd9da8754509e9fd06b81db5cabd5a631bcdaa2683566d9035a3f77fed4862f
MD5 10eb2af85e2ba14e21c69eb1c8da0239
BLAKE2b-256 1ce7a966a5ddb58d148e55fbebdd98482ca6b80106561016d63ebab5ec70f529

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page