better-cov
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.
Supported source files are Python (.py), JavaScript (.js, .jsx, .mjs, .cjs), and TypeScript (.ts, .tsx, .mts, .cts).
How it works
weighted_score = Σ(line_rate_i × importance_i) / Σ(importance_i)
- Parses a Cobertura XML coverage report.
- Extracts function-level coverage from report methods or source ranges.
- Uses Python's AST or Tree-sitter for JavaScript and TypeScript source analysis.
- Counts resolved runtime imports and re-exports across source files.
- Computes a weighted score, prints it, and exports JSON (and optionally Markdown).
better-cov accepts Cobertura XML only. LCOV and tool-specific JSON formats are not supported.
Installation
# Via pip (recommended)
pip install better-cov
# Via uv
uv add better-cov
# Via pipx (isolated CLI)
pipx install better-cov
Quick start
# Auto-detect the report, source directories, and languages from a project root
better-cov /path/to/your/project
# Explicit paths and language
better-cov --coverage-xml coverage.xml --source-dirs src/ --language python
# Multiple source directories
better-cov --coverage-xml coverage.xml --source-dirs packages/app/src packages/lib/src
With PROJECT_DIR, better-cov checks coverage.xml and then coverage/cobertura-coverage.xml. It uses the root src/ directory when present, otherwise it discovers nested src/ directories for monorepos.
Generate Cobertura coverage
Python / pytest
pytest --cov=src --cov-report=xml:coverage.xml
JavaScript / TypeScript with Jest
Add Cobertura to jest.config.js, jest.config.ts, or the equivalent project configuration:
module.exports = {
collectCoverage: true,
coverageReporters: ['text', 'cobertura'],
};
Jest writes coverage/cobertura-coverage.xml by default.
JavaScript / TypeScript with Vitest
Set coverage.reporter in vitest.config.js or vitest.config.ts:
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
coverage: {
reporter: ['text', 'cobertura'],
},
},
});
Vitest also writes coverage/cobertura-coverage.xml by default.
CLI reference
usage: better_cov [-h] [--coverage-xml PATH] [--source-dirs DIR [DIR ...]]
[--language {auto,python,javascript,typescript}]
[--output PATH] [--min-score PCT] [--min-importance FLOAT]
[--top-n N] [--markdown-output PATH] [PROJECT_DIR]
| Argument | Default | Description |
|---|---|---|
PROJECT_DIR |
— | Project root used to auto-detect the Cobertura report and src/ directories |
--coverage-xml |
coverage.xml |
Path to a Cobertura XML report |
--source-dirs |
src/ |
Directories scanned to compute import counts |
--language |
auto |
auto, python, javascript, or typescript; auto-detection uses file extensions |
--output |
better_cov.json |
JSON export path |
--markdown-output |
— | Optional Markdown export path |
--min-score |
— | Fail with exit code 1 if the score is below this percentage |
--min-importance |
0.1 |
Floor importance for functions with zero imports |
--top-n |
10 |
Number of functions shown in console and Markdown reports |
Language selection maps to these extensions:
| Value | Extensions |
|---|---|
python |
.py |
javascript |
.js, .jsx, .mjs, .cjs |
typescript |
.ts, .tsx, .mts, .cts |
auto |
All of the above, selected per file |
TypeScript module resolution
TypeScript imports are resolved against scanned source files using local tsconfig.json and jsconfig.json files. The resolver supports:
- JSONC configuration,
compilerOptions.baseUrl, andcompilerOptions.paths; - relative or absolute local
extendschains; - project
references, including referenced packages in monorepos; - TypeScript equivalents for
.js,.jsx,.mjs, and.cjsimport specifiers.
Resolution deliberately excludes node_modules. Package-based extends, Node package resolution, and aliases defined only in bundler configuration are not supported; expose local aliases through baseUrl/paths and include their files in --source-dirs.
CI integration
GitHub Action
- name: Run tests with coverage
run: pytest --cov=src --cov-report=xml:coverage.xml
- name: Check weighted coverage
uses: diagngrow/better-cov@main
with:
coverage-xml: coverage.xml
source-dirs: src/
language: auto
min-score: 60
Manual install
- name: Check weighted coverage
run: |
pip install better-cov
better-cov . --language auto --min-score 60
Exit codes: 0 = success, 1 = below threshold, 2 = input file not found.
Output
The console report shows weighted and raw coverage plus the most important functions. better_cov.json contains the aggregate scores, configuration, and per-function coverage and importance; use --markdown-output for a PR-friendly report.
Project structure
better_cov/
├── languages/
│ ├── base.py # Language adapter contracts
│ ├── registry.py # --language selection and extension detection
│ ├── python.py # Python AST analysis and import resolution
│ ├── javascript.py # Tree-sitter JavaScript/JSX analysis
│ ├── typescript.py # Tree-sitter TypeScript/TSX adapter
│ └── typescript_config.py # JSONC config and monorepo path resolution
├── parsers/
│ └── cobertura.py # Cobertura-only coverage parser
├── indicators/
│ ├── base.py # ImportanceIndicator abstract interface
│ └── import_count.py # Multi-language import-count indicator
├── models.py # Shared coverage models
├── scorer.py # Weighted score computation
├── reporter.py # Console, JSON, and Markdown reports
└── cli.py # CLI entry point
Requirements
- Python ≥ 3.10
- Runtime source analysis uses
json5,tree-sitter,tree-sitter-javascript, andtree-sitter-typescript
Metadata
Release files for better-cov 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| better_cov-0.3.0.tar.gz | 124.5 kB | With provenance | Details for better_cov-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| better_cov-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for better_cov-0.3.0-py3-none-any.whl |
Total release size: 162.6 kB
Release files / better_cov-0.3.0.tar.gz
| Download URL | better_cov-0.3.0.tar.gz |
|---|---|
| Size | 124.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f7023c1ab1b3d6669dac17c1360f06c405d59dccd11501a2c56ee5895276baf5
|
|
BLAKE2b-256 checksum How to use checksums |
52c8297e9c83a0608215d14dc7dee20c07d6aa8b27e9f3315ed2459debc644f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 4, 2026.
Transparency logRelease files / better_cov-0.3.0-py3-none-any.whl
| Download URL | better_cov-0.3.0-py3-none-any.whl |
|---|---|
| Size | 38.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dbd1df0f0141340484f1a70312bf12ed0002099b90310a519a7cde2febd55570
|
|
BLAKE2b-256 checksum How to use checksums |
a15b8e841f1a26096ba745662fc99f9e4ab5b7369362f68a76dede4f177508c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 4, 2026.
Transparency logSecurity for better-cov 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for better_cov-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 4, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.