pytest plugin for HTTP testing using JSON files
Project description
pytest-httpchain
A pytest plugin for testing HTTP endpoints.
Overview
pytest-httpchain is an integration testing framework for HTTP APIs based on httpx 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:
mcp: installs MCP server package and its starting script. Details in MCP Server.
Features
Pytest integration
Most of pytest magic can be used: markers, fixtures, other plugins.
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 "{{ var }}" 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:
- to extract data from HTTP response
- to verify HTTP response and values in common data context
- to provide custom authentication for requests
- to call in substitution expressions
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()
{
"substitutions": [
{
"vars": {
"user_id": 1
}
}
],
"stages": [
{
"name": "get_user",
"request": {
"url": "https://api.example.com/users/{{ user_id }}"
},
"response": [
{
"verify": {
"status": 200
}
},
{
"save": {
"jmespath": {
"user_name": "user.name"
}
}
}
]
},
{
"name": "update_user",
"fixtures": ["now_utc"],
"request": {
"url": "https://api.example.com/users/{{ user_id }}",
"method": "PUT",
"body": {
"json": {
"user": {
"name": "{{ user_name }}_updated",
"timestamp": "{{ str(now_utc) }}"
}
}
}
},
"response": [
{
"verify": {
"status": 200
}
}
]
},
{
"name": "cleanup",
"always_run": true,
"request": {
"url": "https://api.example.com/cleanup",
"method": "POST"
}
}
]
}
Scenario we created:
- common data context is seeded with the first variable
user_id - get_user
url is assembled usinguser_idvariable from common data context
HTTP GET call is made
we verify the call returned code 200
assuming JSON body is returned, we extract a value by JMESPath expressionuser.nameand save it to common data context underuser_namekey - update_user
now_utcfixture value is injected into common data context
url is assembled usinguser_idvariable from common data context
we create JSON body in place using values from common data context, note thatnow_utcis converted to string in place
HTTP PUT call with body is made
we verify the call returned code 200 - cleanup
finalizing call meant for graceful exit
always_runparameter means this stage will be executed regardless of errors in previous stages
For detailed examples see USAGE.md.
Configuration
- Test file discovery is based on this name pattern:
test_<name>.<suffix>.json.
Thesuffixis configurable as pytest ini option, default value is http. $refinstructions can point to other files; absolute and relative paths are supported.
You can limit the depth of relative path traversal usingref_parent_traversal_depthini option, default value is 3.
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:
- Scenario validation - validate test scenario and scan for possible problems
Documentation
- Usage Examples - Practical code examples
- Full Documentation - Complete guide
- Changelog - Release notes
Thanks
pytest-httpchain was inspired by Tavern and pytest-play.
httpx does comms.
Pydantic keeps structure.
simpleeval powers templates.
pytest-order sorts chain.
pytest-datadir saved me a lot of elbow grease while testing.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_httpchain-0.2.0.tar.gz.
File metadata
- Download URL: pytest_httpchain-0.2.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1dbb10409a606201798ed09c19eec08dbb117766e5383ede7584422824dae959
|
|
| MD5 |
db069cbe7debd49bd6edb2c9b6f40475
|
|
| BLAKE2b-256 |
f8a6ec3f592f89e00b068f4cd42592f73c29a5fdad03475328db7ddedb437698
|
Provenance
The following attestation bundles were made for pytest_httpchain-0.2.0.tar.gz:
Publisher:
publish.yml on aeresov/pytest-httpchain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_httpchain-0.2.0.tar.gz -
Subject digest:
1dbb10409a606201798ed09c19eec08dbb117766e5383ede7584422824dae959 - Sigstore transparency entry: 803889733
- Sigstore integration time:
-
Permalink:
aeresov/pytest-httpchain@3a1635c0c1af289017f42dc64a64cfc2f66566b0 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/aeresov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3a1635c0c1af289017f42dc64a64cfc2f66566b0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pytest_httpchain-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pytest_httpchain-0.2.0-py3-none-any.whl
- Upload date:
- Size: 16.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb0cdcf36e1c54275f9005b6a5d237f575b827bcd9b119a689fb83bfd9505982
|
|
| MD5 |
b7e67f0897ad9c5f4b3f2c143aca7522
|
|
| BLAKE2b-256 |
c4ce03346e02bebf1aecb37812d1346239628c38fdb31b5ac8a4cef554f7aa78
|
Provenance
The following attestation bundles were made for pytest_httpchain-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on aeresov/pytest-httpchain
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_httpchain-0.2.0-py3-none-any.whl -
Subject digest:
fb0cdcf36e1c54275f9005b6a5d237f575b827bcd9b119a689fb83bfd9505982 - Sigstore transparency entry: 803889794
- Sigstore integration time:
-
Permalink:
aeresov/pytest-httpchain@3a1635c0c1af289017f42dc64a64cfc2f66566b0 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/aeresov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3a1635c0c1af289017f42dc64a64cfc2f66566b0 -
Trigger Event:
release
-
Statement type: