How do I make coverage include not tested files?


I have just started writing some unit tests for a python project I have using unittest and coverage. I’m only currently testing a small proportion, but I am trying to work out the code coverage

I run my tests and get the coverage using the following

python -m unittest discover -s tests/
coverage run -m unittest discover -s tests/
coverage report -m

The problem I’m having is that coverage is telling I have 44% code coverage and is only counting the files that:

  1. were tested in the unit tests (i.e., all the files that were not tested are missing and not in the overall coverage)

  2. were in the libraries in the virtual environment and code coverage of the actual tests too. Surely it should not be including the actual tests in the results?

Furthermore, it says the files that are actually tested in these unit tests only have the first few lines tested (which are in most cases the import statements)

How do I get a more realistic code coverage or is this how it is meant to be?

Asked By: user4591756



If you use nose as a testrunner instead, the coverage plugin for it provides

  --cover-inclusive     Include all python files under working directory in
                        coverage report.  Useful for discovering holes in test
                        coverage if not all files are imported by the test
                        suite. [NOSE_COVER_INCLUSIVE]

  --cover-tests         Include test modules in coverage report
Answered By: Daenyth

Add --source=. to the coverage run line. It will both limit the focus to the current directory, and will search for .py files that weren’t run at all.

Answered By: Ned Batchelder

Adding --source=. won’t work if you don’t have explicit files in every directory containing .py files.

As Ned Batchelder pointed out in a comment to his answer, looks for these files to know if the contents of directory are importable. (code src on github)

Follow up reading:
github pytest-cov issue
github coveragepy issue
solution of enforcing files

Answered By: Igor KoĊ‚akowski

You can find this in coverage docs

add the following to coveragerc

include_namespace_packages = True

(This might include some unwanted packages as well, use omit config to remove those)

Answered By: Vaibhav