Test Coverage¶
This project uses pytest-cov to track test coverage. To generate a coverage report:
python -m pytest --cov=mastoscore --cov-report=html
This will create an HTML coverage report in the htmlcov/
directory. Open htmlcov/index.html
in a web browser to see it.
Understanding Coverage Reports¶
The coverage report shows:
- Line coverage: The percentage of code lines that were executed during tests
- Branch coverage: The percentage of code branches (if/else statements) that were executed
- Missing lines: Specific lines of code that weren't executed during tests
Improving Coverage¶
To improve test coverage:
- Identify uncovered code: Use the coverage report to find modules and functions with low coverage
- Write targeted tests: Create tests specifically for uncovered functions
- Test edge cases: Make sure your tests cover different branches of conditional statements
- Mock dependencies: Use mocking to test functions that depend on external services
Coverage Goals¶
For Mastoscore, aim for:
- At least 80% overall code coverage
- 100% coverage for critical modules like
config.py
andanalyse.py
- Test all public functions and methods
Excluding Code from Coverage¶
If code doesn't need to be tested or is difficult to test, exclude it from coverage reports using comments:
def difficult_to_test_function():
try:
# Some code that's hard to test
pass
except Exception: # pragma: no cover
# This exception handler won't be counted in coverage
pass
Coverage Configuration¶
You can configure coverage settings in a .coveragerc
file:
[run]
source = mastoscore
omit =
*/tests/*
*/site-packages/*
[report]
exclude_lines =
pragma: no cover
def __repr__
raise NotImplementedError
This configuration:
- Specifies that we only want to measure coverage for the mastoscore
package
- Omits test files and site-packages from coverage measurement
- Excludes certain lines from coverage reporting