Skip to content

Latest commit

 

History

History
79 lines (49 loc) · 2.46 KB

File metadata and controls

79 lines (49 loc) · 2.46 KB

Specmatic Sample: Python-Sanic BFF

This example project illustrates the practice of contract-driven development and contract testing within a Sanic (Python) application that relies on an external domain service. In this context, Specmatic is utilized to stub calls to domain API services according to its OpenAPI specification.

Here is the Domain OpenAPI spec

Here is the BFF OpenAPI spec

Definitions

  • BFF: Backend for Frontend
  • Domain API: API managing the domain model
  • Specmatic Stub/Mock Server: Generate a server that simulates a real service using its OpenAPI or AsyncAPI specification

Background

A standard web application setup may resemble the following structure. By leveraging Specmatic, we can engage in contract-driven development and perform comprehensive testing on all the components listed below. In this illustrative project, we demonstrate the process for a Sanic BFF, which relies on the Domain API Service, showcasing OpenAPI support within Specmatic.

HTML client talks to client API which talks to backend API

Tech

  1. Sanic
  2. Specmatic
  3. PyTest
  4. Coverage

Setup

  1. Install Python 3.12
  2. Install JRE 17 or later.

Setup Virtual Environment

  1. Create a virtual environment named ".venv" by executing the following command in the terminal from the project's root directory

     python -m venv .venv
  2. Activate virtual environment by executing

  • on MacOS and Linux

    source .venv/bin/activate
  • on Windows CMD

    .venv\Scripts\activate.bat
  • on Windows Powershell (you may need to adjust the ExecutionPolicy)

    .\.venv\Scripts\Activate.ps1

Install Dependencies

To install all necessary dependencies for this project, navigate to the project's root directory in your terminal and execute

pip install -r requirements.txt

Execute Tests and Validate Contracts with Specmatic

Executing this command will initiate Specmatic and execute the tests on the Sanic application.

pytest test -v -s