Instead of using the pytest-cov plugin, use coverage to run pytest:

coverage run -m pytest ....

That way, coverage will be started before pytest.

Answer from Ned Batchelder on Stack Overflow
🌐
Readthedocs
pytest-cov.readthedocs.io › en › latest › reporting.html
Reporting - pytest-cov 7.1.0 documentation
The default is terminal report without line numbers: pytest --cov=myproj tests/ -------------------- coverage: platform linux2, python 2.6.4-final-0 --------------------- Name Stmts Miss Cover ---------------------------------------- myproj/__init__ 2 0 100% myproj/myproj 257 13 94% myproj/feature4286 94 7 92% ---------------------------------------- TOTAL 353 20 94%
🌐
PyPI
pypi.org › project › pytest-cov
pytest-cov · PyPI
Added support for LCOV output format via –cov-report=lcov. Only works with coverage 6.3+. Contributed by Christian Fetzer in #536. Modernized pytest hook implementation.
      » pip install pytest-cov
    
Published   Mar 21, 2026
Version   7.1.0
People also ask

How to run pytest with coverage?
Install the coverage plugin and run your tests with coverage enabled to measure how much of your code is tested.
🌐
testmu.ai
testmu.ai › testmu ai › blog › what is pytest coverage & generate pytest coverage report | testmu ai
What is Pytest Coverage & Generate Pytest Coverage Report | TestMu ...
What is code coverage in pytest?
Code coverage in pytest measures the amount of Python code executed while the tests run. It helps identify which parts of your codebase have not been tested and thus might contain hidden bugs.
🌐
testmu.ai
testmu.ai › testmu ai › blog › what is pytest coverage & generate pytest coverage report | testmu ai
What is Pytest Coverage & Generate Pytest Coverage Report | TestMu ...
Top answer
1 of 3
99

Instead of using the pytest-cov plugin, use coverage to run pytest:

coverage run -m pytest ....

That way, coverage will be started before pytest.

2 of 3
54

You can achieve what you want without pytest-cov.


❯ coverage run --source=<package> --module pytest --verbose <test-files-dirs> && coverage report --show-missing
OR SHORTER
❯ coverage run --source=<package> -m pytest -v <test-files-dirs> && coverage report -m
Example: (for your directory structure)
❯ coverage run --source=plugin_module -m pytest -v tests && coverage report -m
======================= test session starts ========================
platform darwin -- Python 3.9.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/johndoe/.local/share/virtualenvs/plugin_module--WYTJL20/bin/python
cachedir: .pytest_cache
rootdir: /Users/johndoe/projects/plugin_module, configfile: pytest.ini
collected 1 items

tests/test_my_plugin.py::test_my_plugin PASSED               [100%]

======================== 1 passed in 0.04s =========================
Name                            Stmts   Miss  Cover   Missing
-------------------------------------------------------------
plugin_module/supporting_module.py  4      0   100%
plugin_module/plugin.py             6      0   100%
-------------------------------------------------------------
TOTAL                              21      0   100%

For an even nicer output, you can use:

❯ coverage html && open htmlcov/index.html


Documentation

❯ coverage -h
❯ pytest -h

coverage

run -- Run a Python program and measure code execution.

-m, --module --- Show line numbers of statements in each module that weren't executed.

--source=SRC1,SRC2, --- A list of packages or directories of code to be measured.

report -- Report coverage stats on modules.

-m, --show-missing --- Show line numbers of statements in each module that weren't executed.

html -- Create an HTML report.

pytest

-v, --verbose -- increase verbosity.

🌐
Readthedocs
pytest-cov.readthedocs.io › en › latest › config.html
Configuration - pytest-cov 7.1.0 documentation
This plugin provides a clean minimal set of command line options that are added to pytest. For further control of coverage use a coverage config file. ... Path or package name to measure during execution (multi-allowed). Use --cov= to not do any source filtering and record everything. ... Reset cov sources accumulated in options so far. ... Type of report to generate: term, term-missing, annotate, html, xml, json, markdown, markdown-append, lcov (multi-allowed).
🌐
Pytest with Eric
pytest-with-eric.com › pytest-best-practices › pytest-code-coverage-reports
How To Generate Beautiful & Comprehensive Pytest Code Coverage Reports (With Example) | Pytest with Eric
December 1, 2022 - In simple terms, a Coverage Report shows how a % measure of code that has been validated using a Unit Testing Framework (e.g. Unittest or Pytest).
🌐
Readthedocs
coverage.readthedocs.io
Coverage.py — Coverage.py 7.13.5 documentation
It can also measure branch coverage. It can tell you what tests ran which lines. It can produce reports in a number of formats: text, HTML, XML, LCOV, and JSON.
🌐
GitHub
github.com › pytest-dev › pytest-cov
GitHub - pytest-dev/pytest-cov: Coverage plugin for pytest. · GitHub
This plugin provides coverage functionality as a pytest plugin. Compared to just using coverage run this plugin does some extras: Automatic erasing and combination of .coverage files and default reporting.
Starred by 2K users
Forked by 232 users
Languages   Python
Find elsewhere
🌐
Testmu
testmu.ai › testmu ai › blog › what is pytest coverage & generate pytest coverage report | testmu ai
What is Pytest Coverage & Generate Pytest Coverage Report | TestMu AI (Formerly LambdaTest)
January 11, 2026 - Learn what Pytest Coverage is and how to generate a Pytest Coverage report to track untested code and improve Python test reliability.
🌐
BrowserStack
browserstack.com › home › guide › how to generate pytest code coverage report
How to Generate Pytest Code Coverage Report | BrowserStack
July 3, 2025 - The pytest-cov is a plugin for creating code coverage reports with pytest. It is bundled with support for Pytest and has command-line capabilities. It is similar to coverage.py, but it offers more functionalities like subprocess support, xdist ...
🌐
Theodore Manassis
mamonu.github.io › testing_coverage
Using pytest and creating a testing coverage report from it - Theodore Manassis
May 30, 2019 - Today I will try to show a practical demo of using pytest. Furthermore I will show we can be also produce coverage information in the form of an html report from the the pytest testing suite.
🌐
Codecov
docs.codecov.com › docs › code-coverage-with-python
Code coverage with Python - Quick Start
In this tutorial, we’ll use pytest -cov to generate a code coverage report locally.
🌐
Scientific Python Development
learn.scientific-python.org › development › guides › coverage
Code coverage - Scientific Python Development Guide
python -m pytest -ra --cov=vector --cov-config=pyproject.toml · --cov option will also print a minimal coverage report in the terminal itself!
🌐
James Madison University
w3.cs.jmu.edu › cs149 › f24 › info › coverage
Run Tests with Coverage - CS-149-F24
The above results show that 68% of the statements in triangles.py were covered by the tests. To see which lines were not tested, you can use the term-missing ("show missing lines in the terminal") coverage report: ... !pytest -q --cov --cov-report=term-missing ...
🌐
DEV Community
dev.to › iamibi › add-coverage-report-with-pytest-and-gitlab-ci-3e9p
Add coverage report with pytest and Gitlab CI - DEV Community
May 26, 2023 - Share Post via... Report Abuse ... In this blog, I am going to cover the integration of Pytest with Poetry and then using the coverage to generate a report that is displayed as part of Gitlab file diffs.
🌐
GitHub
github.com › marketplace › actions › pytest-coverage-comment
Pytest Coverage Comment - GitHub Marketplace
The --cov and --cov-report flags are provided by pytest-cov, not pytest itself. ... - name: Install dependencies run: | pip install pytest pytest-cov - name: Run tests with coverage run: | pytest --cov=src --cov-report=term-missing tests/
🌐
SonarSource
docs.sonarsource.com › sonarqube-server › 9.8 › analyzing-source-code › test-coverage › python-test-coverage
Python test coverage | SonarQube Server 9.8 | Sonar Documentation
March 17, 2026 - Configure the scanning step of ... up the report file from that defined path. The details of setting up coverage within your build process depend on which tools you are using. In our example we use: ... GitHub Actions, to perform the build. Alternatively, we could start the test by invoking the Coverage.py tool (the command coverage) with the pytest invocation ...
🌐
Medium
martinxpn.medium.com › test-coverage-in-python-with-pytest-86-100-days-of-python-a3205c77296
Test Coverage in Python with Pytest (86/100 Days of Python) | by Martin Mirakyan | Medium
April 10, 2023 - Cover: The percentage of statements that were executed during testing. In this example, the my_package/__init__.py module has 100% coverage, meaning that all statements in the module were executed during testing.
Top answer
1 of 3
51

I think you also need to specify the directory/file you want coverage for like py.test --cov=MYPKG --cov-report=html after which a html/index.html is generated.

2 of 3
16

if you do not specify --cov=/path/to/code then it will not generate the html at all.

$ py.test --cov-report html test_smoke.py
== test session starts == 
platform linux2 -- Python 2.7.12, pytest-3.4.0, py-1.5.2, pluggy-0.6.0 rootdir: /home/someuser/somedir, inifile: plugins: xdist-1.22.0, forked-0.2, cov-2.5.1 collected 3 items                                                                 


test_smoke.py ...                                             [100%]

== 3 passed in 0.67 seconds ==

We can see that there is no message that output was created... However if we specify --cov=...

$ py.test --cov-report html test_smoke.py --cov=/path/to/code
== test session starts ==
platform linux2 -- Python 2.7.12, pytest-3.4.0, py-1.5.2, pluggy-0.6.0
rootdir: /home/someuser/somedir, inifile:
plugins: xdist-1.22.0, forked-0.2, cov-2.5.1
collected 3 items                                                                                                                                                                                                                                                         

test_smoke.py ...                                            [100%] 

---------- coverage: platform linux2, python 2.7.12-final-0 ----------
Coverage HTML written to dir htmlcov

We now see that there are no stats for tests that passed, instead we see that coverage was written to HTML and sent to the default directory: ./htmlcov

NOTE: if you want a different directory, then affix :/path/to/directory to the output style html -> py.test --cov-report html:/path/to/htmldir test_smoke.py --cov=/path/to/code

If you see a plain html file, this is an indication that your problem is the --cov=/path/to/my/pkg perhaps... are you sure that the code you are testing lives here?

🌐
OneUptime
oneuptime.com › home › blog › how to configure pytest with coverage reporting on rhel
How to Configure pytest with Coverage Reporting on RHEL
March 4, 2026 - Open htmlcov/index.html in a browser to see an interactive coverage report with highlighted source code. # Generate a Cobertura XML report pytest --cov=mypackage --cov-report=xml tests/