Skip to content

Provider-side demo using consumer-driven contract testing

License

Notifications You must be signed in to change notification settings

sergeyklay/provider-pact-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Provider API Example

Test Contracts Test Code Lint OpenAPI Coverage Status

This is a Python application for explanation of Contract Testing based on Pact.

Here you can find out how to use Pact using the Python language. You can find more of an overview on Pact in the Pact Introduction.

This project uses:

  • Pact, a code-first tool for testing HTTP and message integrations using contract tests
  • pact-python, to create and verify consumer driven contracts
  • OpenAPI, to describe the Products API
  • Flask, a micro web framework for building API

Interaction diagram

Provider

Provider API Example is a sample Flask application that expose endpoints with REST standard. As an example, this project uses the simple Products API. Here is the OpenAPI spec describes the interaction of clients with the provider.

Consumer

For the purity of the experiment, the consumer is implemented as a separate project and can be found at the following repo.

Pact

Sample contracts (pacts) are located here: https://github.com/sergeyklay/consumer-pact-example/tree/main/tests/pacts

Getting Started

Prerequisites

What kind of things you need to install on your workstation to start:

  • Python >= 3.11
  • SQLite3
  • Node.js >= 16

Installing

First, install Python dependencies for provider (Products API):

$ make init
$ make install

Create provider configuration:

$ cp .env.example .env

Run database migrations for provider:

$ make migrate

Add provider seed data to the database:

$ make seed

Next, install Node.js linters and tools:

$ npm install

Run API server

To run API server use the command as follows:

$ make serve

Run tests

To run unit tests use the command as follows:

$ make test

To verify contracts (pacts) use the command as follows:

$ ./tests/run-pytest.sh

Note that before the contracts verification, you must have deployed the broker, as well as the contracts must be published.

Run lint check

To run OpenAPI spec checking use the command as follows:

$ npm run lint

Project Information

Provider API Example is released under the MIT License, and its code lives at GitHub. It’s rigorously tested on Python 3.11+.

If you'd like to contribute to Provider API Example you're most welcome!

Support

Should you have any question, any remark, or if you find a bug, or if there is something you can't do with the Provider API Example, please open an issue.