Python · Testing · FastAPI
Structuring pytest fixtures for FastAPI services
When learners first wire pytest into a FastAPI project, fixtures often become a tangle of session-scoped database handles. We teach a layered approach: factory fixtures for data, client fixtures for HTTP, and explicit markers for integration tests that need a real Postgres container.
In week two of Python for Software Engineering, mentors review pull requests that separate unit tests from API contract tests. The goal is not maximum coverage numbers but tests that fail for the right reasons when auth middleware changes.
A common pitfall is sharing mutable state between tests. We provide a template repository where each test module resets schema via transactional rollbacks. Learners in Deokjin-gu who attend blended labs can pair on debugging flaky tests during office hours.
By the end of the module, participants document which tests belong in CI versus nightly pipelines — a decision hiring panels appreciate when they scan your README.