Coverage Threshold

0.6.2 · active · verified Fri Apr 17

coverage-threshold is a Python library that provides command-line and programmatic tools to enforce coverage percentage limits (lines, branches) based on data generated by the `coverage.py` library. It's currently at version 0.6.2 and has a moderate release cadence, focusing on stability and integration with CI/CD pipelines.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `coverage-threshold` programmatically with mock coverage data. In a real application, the `CoverageReport` object would be generated by parsing an actual `.coverage` file after `coverage.py` has run. The `check_all_thresholds` function raises an exception if any thresholds are not met.

from coverage_threshold.lib import check_all_thresholds
from coverage_threshold.model import Config, CoverageReport

# Simulate a coverage report object
# In a real scenario, this data would be parsed from a .coverage file
mock_report = CoverageReport(
    line_coverage=95.0,
    branch_coverage=85.0,
    file_line_coverage={
        "app.py": 95.0,
        "another_module.py": 90.0
    },
    file_branch_coverage={
        "app.py": 85.0,
        "another_module.py": 80.0
    }
)

# Define the desired thresholds (mimicking pyproject.toml configuration)
config = Config(
    line_threshold=90.0,
    branch_threshold=80.0,
    exclude=[] # No file exclusions for this example
)

print(f"Checking report: Global Line={mock_report.line_coverage:.1f}%, Global Branch={mock_report.branch_coverage:.1f}%")
print(f"Against thresholds: Line={config.line_threshold:.1f}%, Branch={config.branch_threshold:.1f}%")

try:
    # This function will raise an exception if thresholds are not met
    check_all_thresholds(config, mock_report)
    print("\nSUCCESS: All coverage thresholds met!")
except Exception as e:
    print(f"\nFAILURE: Coverage thresholds not met: {e}")

print("\n--- Testing a failure scenario ---")
failing_report = CoverageReport(
    line_coverage=80.0, # This is below the 90% threshold
    branch_coverage=85.0,
    file_line_coverage={
        "app.py": 80.0,
        "another_module.py": 90.0
    },
    file_branch_coverage={
        "app.py": 85.0,
        "another_module.py": 80.0
    }
)

print(f"Checking failing report: Global Line={failing_report.line_coverage:.1f}%, Global Branch={failing_report.branch_coverage:.1f}%")
print(f"Against thresholds: Line={config.line_threshold:.1f}%, Branch={config.branch_threshold:.1f}%")

try:
    check_all_thresholds(config, failing_report)
    print("\nSUCCESS: All coverage thresholds met!")
except Exception as e:
    print(f"\nFAILURE: Coverage thresholds not met: {e}")

view raw JSON →