Skip to content

Automatically generated integration tests from real traffic — 11 test files, zero manual effort #117

@imLostify

Description

@imLostify

TL;DR

I noticed this repo has no test suite, so I ran Httrace against it — a tool that captures real HTTP traffic and generates pytest tests automatically. It produced 11 test files covering every endpoint in ~10 minutes of setup.


Setup (2 lines of code)

Add the middleware to tutorial/wsgi.py:

from django.core.wsgi import get_wsgi_application
from httrace import WsgiHttraceCaptureMiddleware

application = WsgiHttraceCaptureMiddleware(
    get_wsgi_application(),
    api_key="YOUR_API_KEY",   # free at httrace.com
    service="drf-tutorial",
    sample_rate=1.0,
)

Then just use your app normally — Httrace captures traffic in the background. When you're ready:

pip install httrace-cli
httrace generate --service drf-tutorial --format pytest

What got generated

After firing ~20 requests (CRUD on snippets + users, 200s, 404s, 403s), Httrace produced:

test_get_snippets.py

def test_get_snippets_200(client):
    response = client.get("/snippets/")
    assert response.status_code == 200
    assert response.elapsed.total_seconds() * 1000 < 15  # 3× captured latency
    body = response.json()
    assert body["count"] == 0
    assert body["next"] is None
    assert body["previous"] is None
    assert isinstance(body["results"], list)

test_get_snippets_id.py — 4 variants of 404s captured automatically:

def test_get_snippets_1_404(client):
    response = client.get("/snippets/1/")
    assert response.status_code == 404
    body = response.json()
    assert body["detail"] == "No Snippet matches the given query."

def test_get_snippets_9999_404(client):
    response = client.get("/snippets/9999/")
    assert response.status_code == 404
    body = response.json()
    assert body["detail"] == "No Snippet matches the given query."

test_patch_snippets_id.py — captures both authenticated (200) and unauthenticated (403) variants:

@pytest.fixture
def payload_patch_snippets_1_200():
    return {"linenos": True}

def test_patch_snippets_1_200(client, payload_patch_snippets_1_200):
    response = client.patch("/snippets/1/", json=payload_patch_snippets_1_200)
    assert response.status_code == 200
    body = response.json()
    assert body["owner"] == "admin"
    assert body["linenos"] is True
    assert body["language"] == "python"

test_delete_snippets_id.py

def test_delete_snippets_2_204(client):
    response = client.delete("/snippets/2/")
    assert response.status_code == 204

Plus test_post_snippets.py, test_put_snippets_id.py, test_get_snippets_id_highlight.py, test_get_users.py, test_get_users_id.py, test_get_.py, and a conftest.py with session-scoped httpx.Client and auth fixture stubs.


Full file list generated

File Interactions captured
test_get_snippets.py 1
test_post_snippets.py 2
test_get_snippets_id.py 4
test_get_snippets_id_highlight.py 1
test_put_snippets_id.py 1
test_patch_snippets_id.py 2
test_get_users.py 1
test_get_users_id.py 1
test_delete_snippets_id.py 2
test_get_.py (API root) 1
conftest.py

Why this repo in particular

The DRF tutorial is one of the most-forked DRF resources. A lot of people build real projects off this starting point — and those projects go into production with no tests. Having a reference test suite here would set a better baseline for the ecosystem.

Httrace's free tier covers 50k requests/month. No infrastructure to maintain — just the middleware and a CLI command.

Happy to answer questions or open a PR if there's interest.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions