Meaning
Generates a JSON coverage report of measured code coverage and writes it to the specified output file.
Primary Function
Produce a JSON file containing coverage data (executed lines, missing lines, etc.) for further processing or visualization.
Communicative Purpose
Inform external tools or stakeholders about the extent of code coverage in a machine‑readable format.
Pattern
object.method(keyword_arg=value)
Core Structure
object.method(outfile='file_path.json')
Função primária
Produce a JSON file containing coverage data (executed lines, missing lines, etc.) for further processing or visualization.
Propósito comunicativo
Inform external tools or stakeholders about the extent of code coverage in a machine‑readable format.
Situações de gatilho
After running tests with coverage measurement enabled, when a machine‑readable coverage report is needed for further analysis, reporting, or integration with CI/CD pipelines.
Contextos
Typically used after executing `coverage run` or `pytest --cov`, before generating other report formats (HTML, XML) or uploading to code coverage services.
Padrão
object.method(keyword_arg=value)
Estrutura central
object.method(outfile='file_path.json')
Slots de substituição
outfile: str (path to output JSON file)
Colocados típicos
- coverage.run()
- coverage.report()
- coverage.html_report()
- coverage.xml_report()
Substituições comuns
- Using outfile=None to write JSON to stdout
- adding pretty_print=True for human‑readable JSON
- omitting outfile to stream to stdout.
Erros comuns
Calling json_report before starting coverage measurement, resulting in an empty report; forgetting to stop coverage before generating report, leading to incomplete data; specifying a non‑existent or unwritable output path, causing an IOError; assuming json_report includes source code snippets (it only contains counts).
Similar / contraste
.html_report(outfile='coverage.html') – produces an interactive HTML report; .xml_report(outfile='coverage.xml') – produces Cobertura‑compatible XML; .report() – prints a plain‑text summary to the terminal.
Interferências
Coming from Istanbul/nyc: may expect `nyc report --reporter=json` to write coverage.json; in coverage.py the equivalent is `cov.json_report(outfile='coverage.json')`; forgetting to call `cov.stop()` before reporting is a common mistake when migrating from pytest‑cov where reporting is automatic.
Família do chunk
- coverage reporting methods
Nuance
(1) Do not use json_report if you need a human‑friendly report; choose HTML instead. (2) Writing a large JSON file can be I/O intensive for very large projects; consider streaming to stdout if piping to another process. (3) The JSON report contains only execution counts; source code must be available separately for line‑by‑line annotation.
Efeito pragmático
Enables downstream tools (CI services, badge generators, quality gates) to consume coverage data programmatically, allowing automated pass/fail thresholds and trend tracking.
Dica de memória
Like taking a snapshot of your test coverage and saving it as a JSON file you can later open with any JSON viewer.
Nota
The outfile path can be relative or absolute; ensure the directory exists and is writable. If outfile is omitted, JSON is written to standard output.
Upgrade path
Generate an HTML report for richer visualization (`cov.html_report(outfile='htmlcov/index.html')`) or integrate with CI dashboards.
Log in to save chunks.