pytest-httpbin

2.1.0 · active · verified Fri Apr 17

pytest-httpbin is a pytest plugin that provides fixtures to easily test your HTTP library against a local, self-hosted copy of httpbin.org. It's currently at version 2.1.0 and has an infrequent, release-as-needed cadence, primarily for compatibility updates with newer Python or pytest versions.

Common errors

Warnings

Install

Imports

Quickstart

Create a Python file (e.g., `test_my_app.py`) and define test functions that accept `pytest-httpbin` fixtures like `httpbin`, `httpbin_secure`, and `httpbin_ca_bundle`. Pytest will automatically discover and inject these fixtures. Run the tests using the `pytest` command in your terminal. Ensure `requests` is also installed (`pip install requests`) to make HTTP calls.

import pytest
import requests

def test_get_request(httpbin):
    """Test a simple GET request against the local httpbin instance."""
    print(f"Testing against httpbin at: {httpbin.url}")
    response = requests.get(httpbin.url + '/get')
    response.raise_for_status()
    data = response.json()
    assert data['headers']['Host'] == httpbin.host

def test_post_request(httpbin):
    """Test a POST request."""
    payload = {'key': 'value'}
    response = requests.post(httpbin.url + '/post', json=payload)
    response.raise_for_status()
    data = response.json()
    assert data['json'] == payload

def test_secure_request(httpbin_secure, httpbin_ca_bundle):
    """Test an HTTPS request with CA bundle verification."""
    print(f"Testing against secure httpbin at: {httpbin_secure.url}")
    # Note: requests must be explicitly told to verify with the provided CA bundle
    response = requests.get(httpbin_secure.url + '/get', verify=httpbin_ca_bundle)
    response.raise_for_status()
    data = response.json()
    assert data['headers']['Host'] == httpbin_secure.host

# To run this, save as `test_httpbin_example.py` and run `pytest` in your terminal.

view raw JSON →