Test coverage measures how much code is executed during tests.
1# Install2pip install pytest-cov34# Run with coverage5pytest --cov=src --cov-report=html67# Terminal report8pytest --cov=src --cov-report=term-missing910# .coveragerc configuration11[run]12source = src13omit = */tests/*, */migrations/*1415[report]16fail_under = 8017show_missing = true18exclude_lines =19 pragma: no cover20 def __repr__21 if __name__ == "__main__":
1# src/module.py2def calculate(a, b):3 return a + b # Covered by test45def unused():6 pass # Not covered78# tests/test_module.py9def test_calculate():10 assert calculate(2, 3) == 5
Output:
Name Stmts Miss Cover
-------------------------------------
src/module.py 4 1 75%
-------------------------------------
TOTAL 4 1 75%