Skip to main content

Coverage score weighted by function importance

Project description

better-cov

PyPI version Python License: Apache 2.0

Coverage score weighted by function importance.

Standard line coverage treats every function equally. better-cov gives a higher weight to functions that are actually used — measured by how often they are imported across your codebase — so the score reflects what matters most.

How it works

weighted_score = Σ(line_rate_i × importance_i) / Σ(importance_i)
  1. Parses a coverage.xml (Cobertura format, produced by pytest-cov)
  2. Extracts function-level coverage using AST when method-level data is absent
  3. Counts how many times each function is imported across source files (from module import symbol)
  4. Computes a weighted score — heavily-imported functions have more influence on the final score
  5. Reports results to console and exports a JSON file

Installation

# Via pip (recommandé)
pip install better-cov

# Via uv
uv add better-cov

# Via pipx (CLI isolé)
pipx install better-cov

Quick start

# Auto-detect coverage.xml and src/ from a project root
better-cov /path/to/your/project

# Explicit paths
better-cov --coverage-xml coverage.xml --source-dirs src/

# Multiple source directories
better-cov --coverage-xml coverage.xml --source-dirs src/ lib/ core/

Generate coverage.xml with pytest-cov beforehand:

pytest --cov=src --cov-report=xml

CLI reference

usage: better_cov [-h] [--coverage-xml PATH] [--source-dirs DIR [DIR ...]]
                  [--output PATH] [--min-score PCT] [--min-importance FLOAT]
                  [--top-n N] [PROJECT_DIR]
Argument Default Description
PROJECT_DIR Project root: auto-detects coverage.xml and src/ subdirectories
--coverage-xml coverage.xml Path to the Cobertura XML report
--source-dirs src/ Directories scanned to compute import counts
--output better_cov.json JSON export path
--min-score Fail with exit code 1 if score is below this threshold (useful in CI)
--min-importance 0.1 Floor importance for functions with zero imports
--top-n 10 Number of functions shown in the console report

CI integration

GitHub Action (recommended)

- name: Run tests with coverage
  run: pytest --cov=src --cov-report=xml

- name: Check weighted coverage
  uses: diagngrow/better-cov@main
  with:
    coverage-xml: coverage.xml
    source-dirs: src/
    min-score: 60

Manual install

- name: Run tests with coverage
  run: pytest --cov=src --cov-report=xml

- name: Check weighted coverage
  run: |
    pip install better-cov
    better-cov --coverage-xml coverage.xml --source-dirs src/ --min-score 60

Exit codes: 0 = success, 1 = below threshold, 2 = input file not found.

Output

Console

  Weighted Coverage Report
──────────────────────────────────────────────────────────────
  Weighted score    52.0%  [██████████░░░░░░░░░░]
  Raw coverage      43.5%  [█████████░░░░░░░░░░░]
  Difference        +8.5%

  Indicators : import_count
  Functions  : 144

  Top 10 most important functions

  File                                  Function                    Coverage  Importance
  mypackage/helpers.py                  process_response             100.0%     0.500
  mypackage/core.py                     load_model                    95.8%     0.500
  mypackage/helpers.py                  normalize_response           100.0%     0.375
  ...

JSON (better_cov.json)

{
  "generated_at": "2026-05-21T15:00:00+00:00",
  "global_score": 0.52,
  "global_score_pct": 52.0,
  "raw_coverage": 0.435,
  "total_functions": 144,
  "indicators": ["import_count"],
  "functions": [
    {
      "file": "mypackage/helpers.py",
      "function": "process_response",
      "line_rate": 1.0,
      "lines_covered": 5,
      "lines_total": 5,
      "importance": 0.5,
      "weighted_contribution": 0.5,
      "indicator_scores": { "import_count": 0.5 }
    }
  ]
}

Project structure

better_cov/
├── parsers/
│   └── cobertura.py        # Cobertura XML parser + AST function extraction
├── indicators/
│   ├── base.py             # ImportanceIndicator abstract interface
│   └── import_count.py     # Import-count indicator
├── scorer.py               # Weighted score computation
├── reporter.py             # Console report + JSON export
└── cli.py                  # CLI entry point

Requirements

  • Python ≥ 3.12
  • No runtime dependencies (stdlib only)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

better_cov-0.1.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

better_cov-0.1.0-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file better_cov-0.1.0.tar.gz.

File metadata

  • Download URL: better_cov-0.1.0.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for better_cov-0.1.0.tar.gz
Algorithm Hash digest
SHA256 803acf1cf79e0d5053b7f00ae8f2cd611d6675a314cf600dfbac07cb7fa714c9
MD5 66c930180432ddf0af01a2099a93c58e
BLAKE2b-256 025ee4d9d7073f7a5a8ae7708bdcf5f615b208aebe0be7540897afb0f54c9f5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for better_cov-0.1.0.tar.gz:

Publisher: release.yml on diagngrow/better-cov

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file better_cov-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: better_cov-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for better_cov-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a432411a4d75a3d163134c93ed1cd4c6172641bb07ff5b6fab032a6bebeaa9f1
MD5 19f8f65a0a529dfed07123f866fbcb21
BLAKE2b-256 89e4780a3c7ab2028c93fd129e9b1022878558f2911f07874509887f61c77bc5

See more details on using hashes here.

Provenance

The following attestation bundles were made for better_cov-0.1.0-py3-none-any.whl:

Publisher: release.yml on diagngrow/better-cov

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page