This example project illustrates the practice of contract-driven development and contract testing within a Flask (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
- 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
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 Flask BFF, which relies on the Domain API Service, showcasing OpenAPI support within Specmatic.
- Flask
- Specmatic
- PyTest
- Coverage
- Install Python 3.12
- Install JRE 17 or later.
-
Create a virtual environment named ".venv" by executing the following command in the terminal from the project's root directory
python -m venv .venv
-
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
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
Executing this command will initiate Specmatic and execute the tests on the Flask application.
pytest test -v -s