Part 5: Testing with pytest and Modern Packaging
Introduction
pytest Basics
Installation
pip install pytest pytest-asyncio httpxMinimal test
# tests/unit/test_converter.py
def add(a: int, b: int) -> int:
return a + b
def test_add() -> None:
assert add(2, 3) == 5
def test_add_negative() -> None:
assert add(-1, 1) == 0Assertions — just use assert
assertFixtures
Basic fixture
Fixture scope
Fixture with teardown
conftest.py — shared fixtures across files
conftest.py — shared fixtures across filesAsync Tests with pytest-asyncio
pytest-asyncioAsync fixtures
Testing FastAPI with httpx.AsyncClient
httpx.AsyncClientMocking with unittest.mock
unittest.mockpytest-mock — cleaner mock API
pytest-mock — cleaner mock APIParametrize — test multiple inputs
Test Organisation
Code Quality Tools
ruff — linter + formatter (replaces flake8 + black)
ruff — linter + formatter (replaces flake8 + black)mypy — static type checking
mypy — static type checkingMakefile bringing it together
GitHub Actions CI
Publishing to PyPI
Full pyproject.toml Reference
pyproject.toml ReferenceSummary
Concept
Key point
Series Complete
Part
Topic
Last updated