Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk6 min

How to Generate a Pytest Code Coverage Report

Run pytest-cov to get a terminal coverage summary, identify unexecuted lines, or generate browsable HTML and machine-readable reports.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Install pytest-cov, then run pytest --cov=YOUR_PACKAGE tests/. Replace YOUR_PACKAGE with the importable package or source path you want to measure, and tests/ with your test directory. This produces a coverage summary in the terminal. To see unexecuted line numbers and create a browsable HTML report, run:

python -m pip install pytest-cov
pytest --cov=YOUR_PACKAGE --cov-report=term-missing --cov-report=html tests/

The HTML report is written to htmlcov/ by default; open htmlcov/index.html in a browser. The instructions below cover report formats, source selection, repeatable configuration, and common problems. The current stable pytest-cov documentation is for version 7.1.0, updated March 21, 2026.

Install pytest-cov and run a first report

  1. Install the plugin into the same Python environment used to run your tests:

    python -m pip install pytest-cov
  2. From the project root, run pytest with the package or path to measure:

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
    pytest --cov=YOUR_PACKAGE tests/
  3. Read the terminal summary. The default output reports statements, missed statements, and a coverage percentage. It is a line-coverage measure; it does not by itself show which uncovered line numbers need attention.

pytest-cov is the pytest plugin that collects coverage while the test suite runs. The project README provides the installation and basic invocation pattern: pytest-cov on GitHub.

Show missing lines and create an HTML report

For a more useful local report, request both terminal detail and HTML output in the same test run:

pytest --cov=YOUR_PACKAGE 
  --cov-report=term-missing 
  --cov-report=html 
  tests/

term-missing adds uncovered line numbers to the terminal summary. The HTML report is created in htmlcov/ unless you specify another destination. Open htmlcov/index.html to navigate the files and inspect coverage by line.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

pytest-cov supports several report types and destinations; see its reporting documentation for current syntax and options.

Choose a report format and destination

Format Command option Typical use
Terminal summary --cov-report=term Quick result in a local shell or CI log.
Terminal with missing lines --cov-report=term-missing See which line numbers were not executed. Add :skip-covered to omit files with full coverage.
HTML --cov-report=html or --cov-report=html:coverage-html Browse annotated results locally. The destination is a directory.
XML --cov-report=xml or --cov-report=xml:coverage.xml Provide a file for a downstream tool expecting XML.
JSON --cov-report=json or --cov-report=json:coverage.json Feed structured coverage data to another process.
Markdown --cov-report=markdown:coverage.md Write a Markdown report; append mode is also supported.
LCOV --cov-report=lcov:coverage.info Provide an LCOV file for a compatible consumer.
Annotated source --cov-report=annotate:coverage-annotated Create annotated source output in a directory.

Once you specify any --cov-report option, pytest-cov does not add its default terminal report automatically. Include --cov-report=term or --cov-report=term-missing when you also want terminal output. To collect coverage data without producing a report during that run, use the empty option --cov-report=.

Example: terminal details, HTML, and XML together

pytest --cov=YOUR_PACKAGE 
  --cov-report=term-missing 
  --cov-report=html:coverage-html 
  --cov-report=xml:coverage.xml 
  tests/

This writes the HTML report to the coverage-html/ directory and XML to coverage.xml, while printing missing-line details. Other report destinations work the same way: TYPE:DEST. HTML and annotate destinations are directories; XML, JSON, Markdown, and LCOV destinations are files.

Choose the code to measure

Use --cov=YOUR_PACKAGE to select the package or path whose execution should count. Replace the example name with your actual importable package or source location; do not leave the placeholder in the command. You can pass multiple --cov values when you intentionally want to measure more than one target.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If your coverage configuration already defines source, a valued --cov=... option overrides that setting. In that case, use bare --cov to let the configured source selection apply rather than duplicating it on the command line. See pytest-cov’s configuration documentation for the interaction between pytest-cov and coverage configuration.

Make coverage run on every pytest invocation

To avoid retyping the options, add them to pytest’s configuration. For example, in pyproject.toml:

[tool.pytest.ini_options]
addopts = "--cov=YOUR_PACKAGE --cov-report=term-missing"

Replace YOUR_PACKAGE with your project’s target. Because --cov takes an optional value, do not leave it as the last token in addopts if it could consume the next command-line argument. If you deliberately need an empty value, write --cov=.

Projects may have multiple configuration files, such as tox.ini, pyproject.toml, and setup.cfg. If the wrong coverage settings appear to be active, specify the intended file with --cov-config=PATH. The special default name .coveragerc can also lead coverage.py to check other supported configuration files. Subprocesses or tests that change working directory may need an explicit configuration path as well.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Measure branches, enforce a minimum, or combine runs

Branch coverage

Line coverage records whether executable lines ran. Branch coverage also measures alternate control-flow paths. Enable it for a run with:

pytest --cov=YOUR_PACKAGE --cov-branch tests/

It can also be enabled in coverage configuration under [run] with the branch setting. Branch coverage answers a different question from line coverage, so use it when exercising alternatives in conditions and control flow matters to your team.

Fail below a coverage threshold

Use --cov-fail-under=MIN to make pytest-cov fail when the total percentage is below your chosen minimum. For example, a project choosing a 90 percent gate could run:

pytest --cov=YOUR_PACKAGE --cov-fail-under=90 tests/

The threshold is a project policy, not a recommended universal target. coverage.py documents a status code of 2 when its reporting --fail-under threshold is missed; pytest-cov also documents its corresponding threshold option. Check the coverage.py reporting reference when integrating reporting behavior into CI.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Append data from separate runs

pytest-cov normally starts with clean coverage data for a run. Add --cov-append only when you intentionally want to accumulate results from multiple runs. The resulting data file can then be inspected with normal coverage tools.

Keep context about which test ran

For test-level context in coverage data, use --cov-context=test. pytest-cov documents dynamic contexts that include test names and parametrization, which can help investigate which tests exercised a line.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot missing or unexpected output

Or skip the browser setup:

If you also need website screenshots for test documentation or another developer workflow, ScreenshotNeo provides a screenshot API and MCP server. One GET request returns an image or PDF; for example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for parameters. It removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000. Learn more at ScreenshotNeo, or sign up free for 1,000 screenshots a month with no card.

Frequently Asked Questions

Does pytest-cov measure branch coverage by default?

No. Enable it explicitly with --cov-branch or the branch setting in coverage configuration.

Can pytest-cov create more than one report in a run?

Yes. Repeat --cov-report with the formats and destinations you need; include a terminal report option if you also want terminal output.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.