pytest-httpchain

image image image

pytest-httpchain

A pytest plugin for testing HTTP endpoints.

Overview

pytest-httpchain is an integration testing framework for HTTP APIs based on battle-hardened requests lib.
It aims at helping with common HTTP API testing scenarios, where user needs to make several calls in specific order using data obtained along the way, like auth tokens or resource ids.

Installation

Install normally via package manager of your choice from PyPi:

pip install pytest-httpchain

or directly from Github, in case you need a particular ref:

pip install 'git+https://github.com/aeresov/pytest-httpchain@main'

Optional dependencies

The following optional dependencies are available:

Features

Pytest integration

Most of pytest magic can be used: markers, fixtures, other plugins.\

NOTE: parametrization is not yet implemented, therefore parametrize marker won’t have any effect.

Declarative format

Test scenarios are written declaratively in JSON files.
pytest-httpchain supports JSONRef, so use can reuse arbitrary parts of your scenarios with $ref directive.
Properties are merged in a greedy way with type checking.

Multi-stage tests

Each test scenario contains 1+ stages; each stage is a single HTTP call.
pytest-httpchain executes stages in the order they are listed in scenario file; one stage failure stops the execution chain.

Common data context and variable substitution

pytest-httpchain maintains key-value data storage throughout the execution.
This storage (“common data context”) is populated with declared variables, fixtures and data saved by stages. The data remains there throughout the scenario execution.
Writing scenarios, you can use Jinja-style expressions like "" for JSON values. pytest-httpchain does variable substitution dynamically right before executing a stage, and uses common data context keys as variables in these expressions.
Values from common data context also might be verified during verified/asserted.

User functions

pytest-httpchain can import and call regular python functions:

JMESPath support

pytest-httpchain can extract values from JSON responses using JMESPath expressions directly.

JSON schema support

pytest-httpchain can verify JSON reponses against user-defined JSON schema.

Quick Start

Create a JSON test file named like test_<name>.<suffix>.json (default suffix is http):

# conftest.py
import pytest
from datetime import datetime

@pytest.fixture
def now_utc():
    return datetime.now()
{
    "vars": {
        "user_id": 1
    },
    "stages": [
        {
            "name": "get_user",
            "request": {
                "url": "https://api.example.com/users/"
            },
            "response": [
                {
                    "verify": {
                        "status": 200
                    }
                },
                {
                    "save": {
                        "vars": {
                            "user_name": "user.name"
                        }
                    }
                }
            ]
        },
        {
            "name": "update_user",
            "fixtures": ["now_utc"],
            "request": {
                "url": "https://api.example.com/users/",
                "method": "PUT",
                "body": {
                    "json": {
                        "user": {
                            "name": "_updated",
                            "timestamp": ""
                        }
                    }
                }
            },
            "response": [
                {
                    "verify": {
                        "status": 200
                    }
                }
            ]
        },
        {
            "name": "cleanup",
            "always_run": true,
            "request": {
                "url": "https://api.example.com/cleanup",
                "method": "POST"
            }
        }
    ]
}

Scenario we created:

For detailed examples see USAGE.md.

Configuration

MCP Server

pytest-httpchain includes an MCP (Model Context Protocol) server to aid AI code assistants.

Installation

The optional dependency mcp installs MCP server’s package and pytest-httpchain-mcp script.
Use this script as call target for your MCP configuration.

Claude Code .mcp.json example:

{
    "mcpServers": {
        "pytest-httpchain": {
            "type": "stdio",
            "command": "uv",
            "args": ["run", "pytest-httpchain-mcp"],
            "env": {}
        }
    }
}

Features

The MCP server provides:

Documentation

Thanks

pytest-httpchain was heavily inspired by Tavern and pytest-play.
Crucial parts rely on requests, pytest-order and Pydantic.