Home > Net >  is it possible to include files in the python coverage report that weren't touched at all
is it possible to include files in the python coverage report that weren't touched at all

Time:10-17

I would like my python coverage report to include files that don't get imported by the tests at all.

so if I have a directory with 3 files in it: test.py module1.py module2.py

and both modules have print(__file__) in them, and test.py has import module1 in it

and then I run the test like this:

$ python3 -m venv venv
$ venv/bin/pip install coverage
$ venv/bin/coverage run test.py
......../module1.py
$ venv/bin/coverage report
Name         Stmts   Miss  Cover
--------------------------------
module1.py       1      0   100%
test.py          1      0   100%
--------------------------------
TOTAL            2      0   100%

but what would be really useful is to be reminded that I have completely forgotten about a file, see module2.py on the list with 0% next to it.

Is that possible? Even when I include that file explicitly, it's not in the report.

CodePudding user response:

From https://coverage.readthedocs.io/en/6.0.2/source.html

You can specify source to measure with the --source command-line switch, or the [run] source configuration value. The value is a comma- or newline-separated list of directories or importable names (packages or modules).

Specifying the source option also enables coverage.py to report on unexecuted files, since it can search the source tree for files that haven’t been measured at all.

Using the same files:

$ coverage run  test.py 
............../module1.py
$ coverage report
Name         Stmts   Miss  Cover
--------------------------------
module1.py       1      0   100%
test.py          1      0   100%
--------------------------------
TOTAL            2      0   100%
$ coverage run --source . test.py 
.............../module1.py
$ coverage report
Name         Stmts   Miss  Cover
--------------------------------
module1.py       1      0   100%
module2.py       1      1     0%
test.py          1      0   100%
--------------------------------
TOTAL            3      1    67%

The second run includes all files in the current directory ..

  • Related