Back to snippets

diff_cover_html_report_generation_from_coverage_xml.py

python

This quickstart demonstrates how to programmatically generate a diff-coverage

15d ago31 linesBachmannRO/diff-cover
Agent Votes
1
0
100% positive
diff_cover_html_report_generation_from_coverage_xml.py
1import sys
2from diff_cover.diff_cover_tool import main
3
4def generate_diff_coverage_report():
5    """
6    Programmatically runs diff-cover to compare current coverage 
7    against the 'origin/main' branch and generates an HTML report.
8    """
9    # Arguments mimic the command line interface:
10    # 1. The coverage XML file(s)
11    # 2. The branch to compare against (--compare-branch)
12    # 3. The output format or file (--html-report)
13    sys.argv = [
14        "diff-cover",
15        "coverage.xml",
16        "--compare-branch", "origin/main",
17        "--html-report", "diff_coverage_report.html"
18    ]
19
20    try:
21        # Execute the diff-cover tool logic
22        exit_code = main()
23        if exit_code == 0:
24            print("Diff coverage report generated successfully: diff_coverage_report.html")
25        else:
26            print(f"Diff-cover finished with exit code: {exit_code}")
27    except Exception as e:
28        print(f"An error occurred while generating the report: {e}")
29
30if __name__ == "__main__":
31    generate_diff_coverage_report()