Diff-Cover is a command-line tool that enhances code review by reporting test coverage and linting violations specifically on new or modified lines within a Git diff. It compares an XML coverage report (e.g., Cobertura, Clover, JaCoCo, or LCov format) with the output of `git diff` to highlight lines that lack test coverage or contain quality issues. The library is currently at version 10.2.0 and maintains an active release cadence with frequent updates and dependency bumps.
pip install diff-coverNo compatibility data collected yet for this library.
This quickstart demonstrates the core functionality of `diff-cover`. First, ensure you have a Git repository and a `coverage.xml` report generated by a tool like `coverage.py` or `pytest-cov`. The `diff-cover` command then compares this report against your Git changes to identify uncovered or problematic lines in the diff.
Upgrade your Python environment to 3.10 or newer. If you must use Python 3.9 or older, pin `diff-cover<10.0.0`.
Ensure `diff-cover` is run from the same working directory where the coverage report (e.g., `coverage.xml`) was generated. Verify that file paths in the coverage report align with your Git repository structure. Consider updating to `diff-cover>=10.2.0` for improved subfolder handling.
Use the `--expand-coverage-report` argument (available for XML reports) to add lines not appearing in the coverage report to them, allowing for a more comprehensive diff coverage analysis. Be aware this workaround assumes coverage tools report untested statements with 0 hits.
Review existing configurations for `include` and `exclude` patterns. Update them to either a string or a list of strings, ensuring they work as intended with the new normalized behavior.
Upgrade to `diff-cover>=9.7.1` to ensure proper compatibility with Ruff, especially with newer versions of Ruff. If unable to upgrade, you might need to configure Ruff to output in a Pylint-compatible format explicitly.
Install required linting tools manually (e.g., `pip install ruff`) alongside `diff-cover`, as `diff-cover` no longer manages their installation through extras.
Ensure that diff-cover is run from the same working directory where your coverage tool (e.g., coverage.py) generated the XML report, so that file paths in the report align with Git's paths. For multi-module projects, use the `--src-roots` argument to specify the correct base directories for your source code.
First, ensure your remote branches are up-to-date by running `git fetch origin`. Then, verify the branch name you are comparing against is correct and exists. You can explicitly specify an existing branch using the `--compare-branch` argument, e.g., `diff-cover coverage.xml --compare-branch=your_base_branch`.
Verify that your test runner is correctly configured to generate a coverage report in a supported XML format and that the path to this report file is correctly passed to the `diff-cover` command. For example, when using `pytest-cov`, generate the report with `pytest --cov --cov-report=xml` and then run `diff-cover coverage.xml`.