Skip to content

Generated integration tests by running real traffic through httraceย #12

@imLostify

Description

@imLostify

Hey ๐Ÿ‘‹ I came across this repo while looking for well-structured DRF ecommerce APIs. I wanted to share something that might save you a lot of time.

I ran the API locally and used Httrace โ€” a tool that captures real HTTP traffic and auto-generates pytest integration tests from it. No test-writing, no mocking, no setup overhead.

Here's what it generated for GET /api/products/{id}/ from live traffic:

# Generated by Httrace โ€” 5 interactions captured
# Service: django-ecommerce-demo

def test_get_api_products_1_200(client):
    response = client.get("/api/products/1/")

    assert response.status_code == 200
    assert response.elapsed.total_seconds() * 1000 < 7  # 3ร— captured latency
    body = response.json()
    assert body["category"] == "Electronics"
    assert body["price"] == "1299.99"
    assert body["quantity"] == 10

def test_get_api_products_999_404(client):
    response = client.get("/api/products/999/")
    assert response.status_code == 404

It also generated tests for /api/products/categories/, /api/user/login/, /api/user/register/, and /api/user/orders/ โ€” 9 files total from a single traffic capture session.

The conftest is auto-generated too, with a session-scoped httpx.Client and a stub auth_headers fixture.


How it works โ€” 3 lines of setup:

# wsgi.py
from httrace import WsgiHttraceCaptureMiddleware
application = WsgiHttraceCaptureMiddleware(
    get_wsgi_application(),
    api_key="ht_...",
    service="django-ecommerce-api",
)

Then run your server normally (gunicorn works great), fire some requests, and run:

httrace generate --service django-ecommerce-api --format pytest

Since this repo doesn't have any tests yet, this could be a quick way to get solid baseline coverage without the usual boilerplate. Happy to put together a PR if that'd be useful.

Best regards
Arik

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      โšก