app/main.py. Then we’ll add pytest-based unit tests that mock database access so tests remain fast and deterministic.

- Small modules with clear, testable boundaries make unit testing straightforward.
- Isolating I/O (like the database) behind functions or context managers enables easy mocking.
- Fast unit tests improve developer feedback loops and catch regressions early.
- The original code was split across many files and included duplicated logic. Below is a representative fragment showing an endpoint that relies on external modules. This fragmentation makes end-to-end tests slow and unit tests brittle.
- Consolidate the core application and database access into a small, well-documented module. This makes it clear where to patch/mocks during tests.
app/main.py):
- Start with uvicorn and test an endpoint:
Unit tests should not hit production or persistent databases. Mock database connections or use a temporary test database to keep unit tests fast and deterministic.
- GitHub Copilot can scaffold tests quickly if you add a focused comment like
# generate unit testsnear your functions or use a Copilot workspace flow. Always review and refine the generated tests to ensure they mock external dependencies and verify behavior (not just happy paths).
- pytest is used in this example because of its concise fixtures and compatibility with unittest-style tests.
- Use
fastapi.testclient.TestClientfor synchronous tests against the FastAPI app.
app/test_main.py)
- These unit tests patch the database access layer (
get_db_connectionorfetch_fake_data) so no real SQLite file is touched.
- Tests patch
app.main.fetch_fake_dataorapp.main.get_db_connectionso they do not access an actual SQLite database. fastapi.testclient.TestClientallows synchronous testing of async endpoints.unittest.mock.patchandMagicMocksimulate connections, cursors, and database errors.
- Install pytest:
- Run tests:
- Intentionally introduce a small bug (for example:
count = request.count + 1) and rerun tests. A failure indicates your tests detect the regression and are valuable.
Note: when showing examples with object literals or placeholders, keep them wrapped in backticks to avoid MDX parsing issues (for example:
{"first_name": "John"} or <NODEPORT>).
Alternative: unittest-based tests
- If you prefer the standard library, write tests with
unittest.TestCase. pytest discovers and runs unittest-style tests too. For database integration tests, use a temporary or in-memory database (do not point tests to production DB).
Do not run unit tests that modify or connect to production databases. Always isolate unit tests from persistent environments and use mocks or test-specific databases.
- Refactor code into a small, testable module with clear I/O boundaries (database access isolated).
- Use pytest + TestClient for fast API endpoint tests.
- Mock database access (
get_db_connectionorfetch_fake_data) so unit tests remain isolated and deterministic. - Use GitHub Copilot to scaffold tests, but review, refine, and add edge-case assertions.
- Keep tests focused: fast, small, and meaningful.
- FastAPI documentation
- pytest documentation
- unittest — Unit testing framework
- GitHub Copilot in Action course