close

How to get coverage reporting when testing a pytest plugin?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to get coverage reporting when testing a pytest plugin in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

How to get coverage reporting when testing a pytest plugin?

  1. How to get coverage reporting when testing a pytest plugin?

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

  2. get coverage reporting when testing a pytest plugin

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

Method 1

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

coverage run -m pytest ....

That way, coverage will be started before pytest.

Method 2

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
coverage HTML Report

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.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read