Skip to content

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

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

  • Python 3.9 or higher
  • A dependency manager (uv recommended, pip also works)

Running the Example

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:

uv run --group test pytest

Using pip

If using the venv module, the steps require are:

  1. Create the virtual environment and then activate it:

    python -m venv .venv
    source .venv/bin/activate  # On macOS/Linux
    .venv\Scripts\activate     # On Windows
    
  2. Install the required dependencies in the virtual environment:

    pip install -U pip  # Pip 25.1 is required
    pip install --group test -e .
    
  3. Run pytest:

    pytest