pytest Intro (Assertions, Discovery)
On this page
Why pytest
pytest keeps tests concise and readable. In production teams, readable tests reduce review time and improve confidence during refactors.
Minimal Test
def add(a: int, b: int) -> int:
return a + b
def test_add():
assert add(2, 3) == 5
Run Tests
pytest -q
Operational Checklist
- Tests are deterministic (no time, network, randomness without control).
- Failures produce actionable messages (assertions are specific).
- Critical logic has unit tests; boundaries have integration tests.
Failure Modes
- Flaky tests: failures come and go, teams stop trusting CI.
- Over-mocking: tests validate mocks, not real behavior.