pycobertura

4.1.0 · active · verified Wed Apr 15

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.

Warnings

Install

Imports

Quickstart

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

view raw JSON →