pycobertura is a Python library and command-line tool for parsing Cobertura XML coverage reports. It can display, filter, and diff coverage reports, highlighting changes in coverage metrics between two reports. The current version is 4.1.0, and it maintains an active, moderate release cadence, with major versions typically aligning with Python version support or significant API changes.
pip install pycoberturaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse Cobertura XML reports using the `Cobertura` class and then how to compute and display the differences in coverage between two reports using the `diff` function. It uses in-memory XML strings for simplicity, but `Cobertura` can also load directly from file paths.
Upgrade Python to 3.7 or newer, or pin `pycobertura<4.0.0` in your dependencies.
Instead of pre-parsing XML, pass the XML file path or the XML content string directly to `Cobertura('path/to/report.xml')` or `Cobertura('<xml>...</xml>')`.Standardize the file paths in your Cobertura reports, e.g., by ensuring your coverage generation tool uses relative paths from a common root or applying path transformations before diffing.
Ensure the Cobertura XML report is well-formed and valid. Use an XML validator or inspect the file for unescaped characters, incorrect tags, or other structural issues. Tools like `lxml` are stricter than some parsers, which `pycobertura` uses internally.
Provide the correct path to your project's source code directory using the `--source1` and `--source2` options for `diff` or ensuring the `Cobertura` object is initialized with a `filesystem` argument pointing to the correct base path when used as a library.
Verify that your Cobertura XML report actually contains the relevant `line-rate` or `branch-rate` attributes at the expected levels (e.g., overall report, package, or class). Ensure you are calling `line_rate()` as a method (with parentheses) as shown in the library usage examples, and not as a direct attribute.
Correct the regular expression pattern to follow Python's regex conventions. For example, a simple asterisk `*` needs to be escaped (`.*`) to match any characters, or a more specific pattern like `.*Search.*` should be used instead of just `*Search`.