Coverage report formats (terminal, HTML, XML) in PyTest - Build an Automation Script
import subprocess import os import xml.etree.ElementTree as ET from pathlib import Path def test_coverage_reports(): package_name = "your_package" # Replace with your actual package name # Run pytest with coverage and terminal report result = subprocess.run( ["pytest", f"--cov={package_name}"], capture_output=True, text=True ) assert result.returncode == 0, "Pytest run failed" assert "coverage" in result.stdout.lower(), "Coverage summary not found in terminal output" # Run pytest with coverage and HTML report result_html = subprocess.run( ["pytest", f"--cov={package_name}", "--cov-report=html"], capture_output=True, text=True ) assert result_html.returncode == 0, "Pytest HTML report run failed" html_report_path = Path("htmlcov/index.html") assert html_report_path.exists(), "HTML coverage report file does not exist" # Simple check that HTML file contains expected tags with open(html_report_path, "r", encoding="utf-8") as f: html_content = f.read() assert "<html" in html_content.lower() and "coverage" in html_content.lower(), "HTML report content invalid" # Run pytest with coverage and XML report result_xml = subprocess.run( ["pytest", f"--cov={package_name}", "--cov-report=xml"], capture_output=True, text=True ) assert result_xml.returncode == 0, "Pytest XML report run failed" xml_report_path = Path("coverage.xml") assert xml_report_path.exists(), "XML coverage report file does not exist" # Parse XML to verify it is well-formed and contains coverage data tree = ET.parse(xml_report_path) root = tree.getroot() assert root.tag == "coverage", "Root tag of XML report is not 'coverage'" # Cleanup generated reports if html_report_path.exists(): for file in html_report_path.parent.glob("**/*"): if file.is_file(): file.unlink() html_report_path.parent.rmdir() if xml_report_path.exists(): xml_report_path.unlink()
This test automates the manual steps to generate coverage reports in three formats using pytest and pytest-cov.
First, it runs pytest with coverage enabled and checks the terminal output for coverage summary text.
Next, it runs pytest again to generate an HTML report, then verifies the 'htmlcov/index.html' file exists and contains expected HTML content.
Then, it runs pytest to generate an XML report and parses the 'coverage.xml' file to confirm it is valid XML with the root tag 'coverage'.
Finally, it cleans up the generated report files to keep the project directory clean.
This approach uses subprocess to run commands, file checks for report existence, and content parsing for validation, following best practices for test automation.
Now add data-driven testing to generate coverage reports for three different package names