|
| 1 | +# Test Coverage with pytest-cov |
| 2 | + |
| 3 | +Test coverage measures which parts of the source code run while the test suite |
| 4 | +is executing. |
| 5 | + |
| 6 | +It can reveal code that no test reaches, but it cannot prove that the tests make |
| 7 | +the right assertions. |
| 8 | + |
| 9 | +## Coverage is a diagnostic tool |
| 10 | + |
| 11 | +A coverage report helps answer questions such as: |
| 12 | + |
| 13 | +- Which source lines ran during the tests? |
| 14 | +- Which branches of a condition were exercised? |
| 15 | +- Is there production code that the test suite never reaches? |
| 16 | + |
| 17 | +It does not answer: |
| 18 | + |
| 19 | +- Are the assertions correct? |
| 20 | +- Are important requirements missing? |
| 21 | +- Are the tests readable and maintainable? |
| 22 | +- Is the program design good? |
| 23 | + |
| 24 | +Coverage provides evidence about test execution, not a complete quality score. |
| 25 | + |
| 26 | +## Add pytest-cov |
| 27 | + |
| 28 | +This project uses pytest-cov to collect coverage while pytest runs. It is a |
| 29 | +development dependency: |
| 30 | + |
| 31 | +```bash |
| 32 | +uv add --group dev pytest-cov |
| 33 | +``` |
| 34 | + |
| 35 | +The package is not needed by users of `text-toolkit`. |
| 36 | + |
| 37 | +## Configure coverage |
| 38 | + |
| 39 | +Coverage.py reads its settings from `pyproject.toml`: |
| 40 | + |
| 41 | +```toml |
| 42 | +[tool.coverage.run] |
| 43 | +branch = true |
| 44 | +source = ["text_toolkit"] |
| 45 | + |
| 46 | +[tool.coverage.report] |
| 47 | +show_missing = true |
| 48 | +``` |
| 49 | + |
| 50 | +The settings mean: |
| 51 | + |
| 52 | +- `source` limits measurement to the example package, |
| 53 | +- `branch` records possible paths through conditions, |
| 54 | +- `show_missing` lists source lines that did not run. |
| 55 | + |
| 56 | +Tests themselves are not included in the percentage. The report measures the |
| 57 | +package behavior that the tests exercise. |
| 58 | + |
| 59 | +## Statement and branch coverage |
| 60 | + |
| 61 | +Statement coverage checks whether executable statements ran. |
| 62 | + |
| 63 | +Branch coverage also checks different paths through decisions. For example, an |
| 64 | +`if` statement may execute both its true and false branches. Running the line |
| 65 | +with the `if` is not enough to prove that both paths were exercised. |
| 66 | + |
| 67 | +The example package has a condition in `count_words`. Tests cover both an empty |
| 68 | +input and text containing words, so both outcomes are represented. |
| 69 | + |
| 70 | +## Run the coverage report |
| 71 | + |
| 72 | +Run: |
| 73 | + |
| 74 | +```bash |
| 75 | +uv run pytest --cov=text_toolkit --cov-report=term-missing |
| 76 | +``` |
| 77 | + |
| 78 | +pytest still runs the same tests. pytest-cov additionally records execution and |
| 79 | +prints a table similar to: |
| 80 | + |
| 81 | +```text |
| 82 | +Name Stmts Miss Branch BrPart Cover Missing |
| 83 | +---------------------------------------------------------------------------- |
| 84 | +src/text_toolkit/__init__.py 2 0 0 0 100% |
| 85 | +src/text_toolkit/text_stats.py 11 0 4 0 100% |
| 86 | +---------------------------------------------------------------------------- |
| 87 | +TOTAL 13 0 4 0 100% |
| 88 | +``` |
| 89 | + |
| 90 | +The important columns are: |
| 91 | + |
| 92 | +- `Stmts`: executable statements found, |
| 93 | +- `Miss`: statements that did not run, |
| 94 | +- `Branch`: branch destinations found, |
| 95 | +- `BrPart`: partially covered branches, |
| 96 | +- `Missing`: source line numbers that did not run. |
| 97 | + |
| 98 | +The exact counts will change when the package changes. |
| 99 | + |
| 100 | +## Why there is no percentage gate |
| 101 | + |
| 102 | +This project intentionally does not configure `fail_under`. |
| 103 | + |
| 104 | +A fixed threshold can encourage tests written only to increase a number. A |
| 105 | +suite can reach 100% coverage while using weak assertions or missing important |
| 106 | +requirements. |
| 107 | + |
| 108 | +The current report happens to show 100%, but that is a property of a very small |
| 109 | +example package. It is not a promise that every future change must preserve a |
| 110 | +perfect score. |
| 111 | + |
| 112 | +When coverage decreases, inspect the missing lines and decide whether they |
| 113 | +represent meaningful untested behavior. Do not add a test merely to satisfy a |
| 114 | +percentage. |
| 115 | + |
| 116 | +## Generated data |
| 117 | + |
| 118 | +Coverage.py stores local measurement data in: |
| 119 | + |
| 120 | +```text |
| 121 | +.coverage |
| 122 | +``` |
| 123 | + |
| 124 | +This file is generated and listed in `.gitignore`. Do not commit it. |
| 125 | + |
| 126 | +## Rule of thumb |
| 127 | + |
| 128 | +Use coverage to find questions worth asking about the test suite. |
| 129 | + |
| 130 | +Do not use the percentage as a substitute for those questions. |
| 131 | + |
| 132 | +## Continue |
| 133 | + |
| 134 | +- [Previous: Static Type Checking with mypy](11_type_checking_with_mypy.md) |
| 135 | +- [Next: Project Checklist](09_checklist.md) |
| 136 | +- [Back to README](../README.md) |
0 commit comments