Preethi Vaidyanathan
Preethi Vaidyanathan

Reputation: 1322

Why does Coverage.py ignore files with no coverage?

I first run

nosetests --with-coverage

So I should have a .coverage file with all the default settings.

Within folder_1, I have file_1.py, file_2.py, and file_3.py

When I cd into folder_1 and run

coverage report

It outputs:

enter image description here

It doesn't generate anything for file_3.py! But then when I run:

coverage report file_3.py

it says:

enter image description here

Does it skip files with no coverage in the report? How can I change it so the report shows me the results of every *.py file?

Upvotes: 9

Views: 3369

Answers (2)

re_arg
re_arg

Reputation: 156

I ran into this same scenario yesterday and lost some time trying make Coverage consider the corresponding to this file_3.py. Ned Batchelder's answer is completely correct and helped me but when handling multiple folder_1 folders in the same level in the hierarchy I'd have to set all of them as source and that is not ideal.

The key is this part of the official doc:

If the source option is specified, only code in those locations will be measured. 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. Only importable files (ones at the root of the tree, or in directories with a __init__.py file) will be considered.

So unexecuted files will only be analysed if you point at them. For this scenario that means two options:

  1. Set your folder directly as source directory running the tests with the flag --source=folder_1 (which is covered on Neds' answer).
  2. If this is a subfolder of a bigger project you can also just set the source folder as the main project folder but then you need to set the directories you want analysed as packages, creating an __init__.py file in them.

For instance if you have:

src/
 folder_1/
   __init__.py
   file_1.py
   file_2.py
   file_3.py

You can just run with the flag --source=src and folder_1 files will be discoverable as well.

Hope that helps someone in the future.

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375574

You need to specify a source directory for coverage.py to find files that have never been executed at all. You can use --source=folder_1 on the command line, or [run] source=folder_1 in your .coveragerc file.

Upvotes: 10

Related Questions