Skip to content

test(fastapi): add PostHog setup, event, and error capture tests - #305

Open
AlexPaiva wants to merge 2 commits into
PostHog:mainfrom
AlexPaiva:alex/fastapi-example-tests
Open

test(fastapi): add PostHog setup, event, and error capture tests#305
AlexPaiva wants to merge 2 commits into
PostHog:mainfrom
AlexPaiva:alex/fastapi-example-tests

Conversation

@AlexPaiva

Copy link
Copy Markdown

Picks up #24, scoped to one example so the pattern stays reviewable. Builds on #304, without which these files ship inside the packaged skill (the first commit here is that fix, and this diff shrinks once it merges).

Covers what the example exists to demonstrate:

  • the lifespan configures the client and flushes on shutdown, and seeds the default user
  • login, signup and the burrito endpoint capture their events with the expected properties, including the running cookie-backed count
  • /api/test-error and /api/trigger-error hand the exception to capture_exception and surface the returned event id, and ?capture=false skips it

Two implementation notes. The routers bind capture at import time, so it is patched at app.routers.api.capture and app.routers.main.capture rather than on the posthog module. capture_exception and flush are read off the module, so those are patched there instead. The fixture is autouse, so no test can reach the network, and a temp SQLite file keeps runs isolated.

httpx triggers a starlette deprecation warning with TestClient, so requirements-dev.txt pins httpx2. Verified in a clean venv, 11 passed with no warnings.

pytest.ini and requirements-dev.txt are skipped for this example. The tests themselves are covered by the global rule in #304.

If this shape works, the other three Python examples (flask, django, python) can follow it.

shouldSkip gets a path relative to the example root, so the global
`/test/` pattern only matches nested dirs like app/test/. A root-level
test/ or tests/ has no leading slash and never matches, so
next-app-router/tests ships its Playwright specs in the skill output.

Adds an anchored regex for the root-level case, plus a regression test
covering nested, root-level and the testing/ near-miss.
Covers what the example exists to demonstrate: the lifespan configures the
client and flushes on shutdown, the page and API routes capture their events
with the right properties, and the error endpoints hand the exception to
capture_exception and surface the returned event id.

The routers bind `capture` at import time, so it is patched where it is used
rather than on the posthog module. The fixture is autouse, so no test can
reach the network, and a temp SQLite file keeps runs isolated.

pytest.ini and requirements-dev.txt are skipped for this example; the tests
themselves are covered by the global root-level test dir rule.
@AlexPaiva
AlexPaiva requested a review from a team as a code owner July 30, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant