liveBook · Manning

Code coverage

As explained in AI-Powered Developer: Build software with ChatGPT and Copilot

Code coverage is the proportion of a program’s code executed when a specified test suite runs, usually expressed as a percentage.

Code coverage measures how much of a program’s code a test suite executes, usually as a percentage. It helps reveal untested areas and evaluate test effectiveness, but high coverage does not guarantee bug-free software. section 6.4

A practical Python coverage workflow is:

  • Install the coverage library with pip install coverage. section 6.4
  • Run the test suite and collect data with coverage run -m pytest. section 6.4
  • Review a text report with coverage report or generate an HTML report with coverage html in the htmlcov directory. section 6.4
  • Use low coverage to identify code needing more tests; 70% is a starting point, while teams may aim higher. section 6.4

Running coverage for a Python project

pip install coverage coverage run -m pytest coverage report coverage html The resulting report shows coverage for each project file and highlights areas that tests do not exercise. section 6.4

Figure 6.3 The code coverage report showing the coverage for each file in our ITAM system project

Inspect the file-by-file coverage report for an IT asset management project. section 6.4

How AI-Powered Developer: Build software with ChatGPT and Copilot covers it

How deep it goes
Defines the percentage measure, explains its limits, and gives a Python collection-and-reporting workflow.
Examples
Uses pytest commands, the coverage library, text reports, HTML reports, and an IT asset management project report.
What it assumes
Assumes a Python project with tests and pytest, while introducing coverage installation, execution, and reporting.
What you can do after
Apply it via coverage and pytest to measure executed code and locate areas needing additional tests.
How it connects
Connects code coverage with unit testing, integration testing, software quality, and uncaught bugs.

Also explained in

Compare how 14 books explain code coverage →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Developer: Build software with ChatGPT and Copilot. The section links point to the parts of the book it draws on.