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 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:

  • 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.

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 "{{ 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:

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/{{ user_id }}"
            },
            "response": [
                {
                    "verify": {
                        "status": 200
                    }
                },
                {
                    "save": {
                        "vars": {
                            "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 heavily inspired by Tavern and pytest-play.
requests does the comms.
Pydantic keeps the structure.
pytest-order powers the chaining.
pytest-datadir saved me a lot of elbow grease.

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.1.1.tar.gz (14.7 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.1.1-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_httpchain-0.1.1.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pytest_httpchain-0.1.1.tar.gz
Algorithm Hash digest
SHA256 63a626033e7c432056840e245a3bc4c23a045a710b21b415f06f9933bd408bd7
MD5 1c7f3ef4fd9ba06779f1b22efb20a2d7
BLAKE2b-256 e0b26bbf2e7e410e5f6c808e436f83271d7ca049b18e55ad82520017a7a1dfaf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_httpchain-0.1.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.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_httpchain-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bda9fa77ec31992d3446d9c338e8315e463002e2fb058d570b1bd8d3b5149b7f
MD5 a3beee9ec57d883159d6c30107dcac92
BLAKE2b-256 e987197350a95094062a190928cf4ee0cbd8a2fe6970752e529be832b86c9706

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_httpchain-0.1.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