Registry / testing / pycobertura

pycobertura

JSON →
library4.1.0pypypi✓ verified 87d ago

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 pycobertura
INSTALL
IMPORT
SIG · PYCOBERTURA
P
pycobertura
testingpythonv4.1.0
Install
2.7s avg
Import
291ms
Disk
32MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.302s · 33.3MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.7s · import 0.281s · 34MB
32MB installed
● package 32MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Cobertura
from pycobertura import Cobertura
diff
from pycobertura import diff
HtmlReporter
from pycobertura.reporters import HtmlReporter

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.

import io from pycobertura import Cobertura, diff # Example Cobertura XML content cobertura_xml_baseline = '''<?xml version="1.0" ?> <coverage branches-covered="0" branches-valid="0" complexity="0" line-rate="0.5" lines-covered="1" lines-valid="2" timestamp="1678886400" version="coverage 6.0.0"> <sources> <source>/path/to/project</source> </sources> <packages> <package line-rate="0.5" branch-rate="0" complexity="0" name="my_module"> <classes> <class name="my_file.py" filename="my_module/my_file.py" line-rate="0.5" branch-rate="0" complexity="0"> <methods/> <lines> <line number="1" hits="1"/> <line number="2" hits="0"/> </lines> </class> </classes> </package> </packages> </coverage>''' cobertura_xml_current = '''<?xml version="1.0" ?> <coverage branches-covered="0" branches-valid="0" complexity="0" line-rate="1.0" lines-covered="2" lines-valid="2" timestamp="1678886500" version="coverage 6.0.0"> <sources> <source>/path/to/project</source> </sources> <packages> <package line-rate="1.0" branch-rate="0" complexity="0" name="my_module"> <classes> <class name="my_file.py" filename="my_module/my_file.py" line-rate="1.0" branch-rate="0" complexity="0"> <methods/> <lines> <line number="1" hits="1"/> <line number="2" hits="1"/> </lines> </class> </classes> </package> </packages> </coverage>''' # Parse the Cobertura reports # Cobertura can take a filename (string) or a string containing the XML baseline_report = Cobertura(cobertura_xml_baseline) current_report = Cobertura(cobertura_xml_current) print(f"Baseline report line rate: {baseline_report.line_rate:.2f}") print(f"Current report line rate: {current_report.line_rate:.2f}") # Calculate the diff between the two reports differences = diff(baseline_report, current_report) print("\n--- Coverage Differences ---") for file_path, file_diff in differences.files.items(): print(f"File: {file_path}") print(f" Line rate change: {file_diff.line_rate_change:.2f}") print(f" Lines covered change: {file_diff.lines_covered_change}") print(f" Lines valid change: {file_diff.lines_valid_change}") # Access changes for specific lines if available if 'my_module/my_file.py' in differences.files: file_lines_changes = differences.files['my_module/my_file.py'].lines for line_number, status in file_lines_changes.items(): print(f" Line {line_number}: {status.name}") # e.g., NO_CHANGE, COVERED, UNCOVERED_NEW
pycobertura --version
Debug
Known issues
breakingPython 3.6 support was dropped in pycobertura v4.0.0. Users on Python 3.6 must remain on pycobertura < 4.0.0 or upgrade their Python interpreter.
fix
Upgrade Python to 3.7 or newer, or pin `pycobertura<4.0.0` in your dependencies.
affects: >=4.0.0
breakingThe `Cobertura` class constructor API changed significantly in v3.0.0. Previously, it expected a pre-parsed `lxml.etree._Element` object. Since v3.0.0, it expects either a filename string or a string containing the XML content directly.
fix
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>')`.
affects: >=3.0.0
gotchaWhen diffing reports, ensure that the file paths referenced within the Cobertura XML (e.g., `<class filename="my_module/my_file.py">`) are consistent between the two reports, or pycobertura may not correctly match files for comparison.
fix
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.
affects: all
Errors
Common errors & fixes
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1
The Cobertura XML report file is malformed, contains invalid characters, or is not valid XML.
fix
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.
pycobertura.filesystem.FileSystem.FileNotFound: No such file or directory: 'path/to/missing/source_file.py'
Pycobertura cannot find a source code file referenced in the Cobertura report, which is often required for commands like `diff` or when generating HTML reports that display highlighted source code.
fix
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.
AttributeError: 'Cobertura' object has no attribute 'line_rate'
You are attempting to access a method or attribute like `line_rate` or `branch_rate` on a `Cobertura` object, but it might be missing from the parsed XML report or the attribute name is incorrect, or the method is being called incorrectly.
fix
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.
Error: Invalid value for "--ignore-regex": Failed to compile regex pattern: unexpected end of pattern
The regular expression provided to the `--ignore-regex` option for filtering files is syntactically incorrect or incomplete.
fix
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`.
Upgrade
Version history
4.1.0latest on PyPI · released Sep 23, 2025
Audit
Dependencies
lxmlrequiredRequired for parsing Cobertura XML reports efficiently.
Agent activity
7 hits · last 30 days
node
6
Resources
pycobertura — pip install pycobertura · libregistry