Assignment 1 โ Build a Full Test Suite for a FastAPI App#
Goal:#
Create a complete test suite for a small FastAPI application. Students will practice:
Unit tests
Async API tests
JWT authentication tests
Mocked async DB testing with SQLAlchemy
Applying a professional test folder structure
Assignment Description#
You are given a FastAPI application (from Unit 06) that contains:
CRUD endpoints for users
Async SQLAlchemy database connection
JWT-protected endpoint
Helper utilities
Your task is to create a production-quality test suite using:
pytestpytest-asynciohttpx.AsyncClientPyJWTMocked in-memory SQLite async DB
Test folder structure best practices
Requirements#
1. Create the test folder structure#
Your project test can be referred to as below (depending on the structure of Unit 06)
tests/
โโโ conftest.py
โโโ factories/
โ โโโ user_factory.py
โโโ utils/
โ โโโ jwt_helpers.py
โโโ unit/
โ โโโ test_helpers.py
โโโ integration/
โ โโโ test_users.py
โ โโโ test_auth.py
โโโ e2e/
โโโ test_full_flow.py
2. Write tests for the following:#
A. UNIT TESTS (no FastAPI, no DB)#
Example: math helpers, pure functions, etc.
B. ASYNC INTEGRATION TESTS#
Using AsyncClient:
Test async GET and POST endpoints
Test error paths (404, 400, etc.)
C. MOCKED ASYNC SQLALCHEMY DB TESTS#
Using an in-memory SQLite database.
Example tasks:
Mock DB session in
conftest.pyTest database CRUD operations via API
Validate isolation between tests
D. JWT AUTH TESTS#
Test the /secure endpoint with:
Valid token
Expired token
Missing authorization header
Invalid/malformed token
Wrong secret-key token
3. End-to-end Test#
Simulate a full workflow:
Create a user
Authenticate (or generate a token)
Access a secured resource
Fetch user data from DB
Expected Output#
A fully working test suite that can run using:
pytest -v
Grading Criteria#
Category |
Points |
|---|---|
Folder structure |
10 |
Unit tests |
10 |
Async API tests |
20 |
Mocked DB tests |
25 |
JWT tests |
25 |
Code quality & readability |
10 |
Total: 100 points