aiohttp and Flask Example¶
This example demonstrates contract testing between an asynchronous aiohttp
-based client (consumer) and a Flask web server (provider). It showcases modern Python patterns including async/await, type hints, and standalone dependency management.
Overview¶
- Consumer: An async HTTP client using aiohttp
- Provider: A Flask web server
- Consumer Tests: Contract definition and consumer testing
- Provider Tests: Provider verification against contracts
Use the above links to view additional documentation within.
What This Example Demonstrates¶
Consumer Side¶
- Async HTTP client implementation with aiohttp
- Consumer contract testing with Pact mock servers
- Handling different HTTP response scenarios (success, not found, etc.)
- Modern Python async patterns
Provider Side¶
- Flask web server with RESTful endpoints
- Provider verification against consumer contracts
- Provider state setup for different test scenarios
- Mock data management for testing
Testing Patterns¶
- Independent consumer and provider testing
- Contract-driven development workflow
- Error handling and edge case testing
- Type safety with Python type hints
Prerequisites¶
Running the Example¶
Using uv (Recommended)¶
We recommend using uv to manage the virtual env and manage dependencies. The following command will automatically set up the virtual environment, install dependencies, and then execute the command within the virtual environment:
Using pip¶
If using the venv
module, the steps require are:
-
Create the virtual environment and then activate it:
-
Install the required dependencies in the virtual environment:
-
Run pytest: