A pytest plugin for testing HTTP endpoints.
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.
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'
The following optional dependencies are available:
mcp
: installs MCP server package and its starting script. Details in MCP Server.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.
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.
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.
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.
pytest-httpchain
can import and call regular python functions:
pytest-httpchain
can extract values from JSON responses using JMESPath expressions directly.
pytest-httpchain
can verify JSON reponses against user-defined JSON schema.
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:
user_id
user_id
variable from common data contextuser.name
and save it to common data context under user_name
keynow_utc
fixture value is injected into common data contextuser_id
variable from common data contextnow_utc
is converted to string in placealways_run
parameter means this stage will be executed regardless of errors in previous stagesFor detailed examples see USAGE.md.
test_<name>.<suffix>.json
.suffix
is configurable as pytest ini option, default value is http.$ref
instructions can point to other files; absolute and relative paths are supported.ref_parent_traversal_depth
ini option, default value is 3.pytest-httpchain
includes an MCP (Model Context Protocol) server to aid AI code assistants.
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": {}
}
}
}
The MCP server provides:
pytest-httpchain
was heavily inspired by Tavern and pytest-play.
Crucial parts rely on requests, pytest-order and Pydantic.