Skip to main content

pytest plugin for HTTP testing using JSON files

Project description

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 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 using user_id variable 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 expression user.name and save it to common data context under user_name key
  • update_user
    now_utc fixture value is injected into common data context
    url is assembled using user_id variable from common data context
    we create JSON body in place using values from common data context, note that now_utc is 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_run parameter 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.
    The suffix is configurable as pytest ini option, default value is http.
  • $ref instructions can point to other files; absolute and relative paths are supported.
    You can limit the depth of relative path traversal using ref_parent_traversal_depth ini 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

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

pytest_httpchain-0.2.1.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_httpchain-0.2.1-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_httpchain-0.2.1.tar.gz.

File metadata

  • Download URL: pytest_httpchain-0.2.1.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

Hashes for pytest_httpchain-0.2.1.tar.gz
Algorithm Hash digest
SHA256 9d60b334b261332fc26f356e9a996b7b85131fd221f1dbb169a5d424df59677c
MD5 f1780817e493ea1d857c9c0cff3264ff
BLAKE2b-256 e53283d1f0186e71b937e9125dd67e4ad3300835b8034db4baaafed689548bcb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_httpchain-0.2.1.tar.gz:

Publisher: publish.yml on aeresov/pytest-httpchain

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_httpchain-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_httpchain-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e0237269eb9f04573e4e4255ab1a82a8d19ed431c4e89d2aefafd528dbb63303
MD5 9179162aff664df53b0db85af558c227
BLAKE2b-256 ab1a22163f64a2c7e615010dab3e0203d8b7df578acdee00f62f48c74746545d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_httpchain-0.2.1-py3-none-any.whl:

Publisher: publish.yml on aeresov/pytest-httpchain

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page