1

I have 2 files in my project (a.py and b.py). a.py file is imported in the unit test file (test_prog.py) and there are tests written for it. Pytest-cov shows the coverage for this file.

However for the other file b.py - it is not seen in the code coverage output. Pytest-cov does not consider a file for coverage analysis unless it is imported in one of the unit tests?

davidism
  • 121,510
  • 29
  • 395
  • 339
variable
  • 8,262
  • 9
  • 95
  • 215
  • Are you using the `--cov` argument? – MrBean Bremen May 26 '20 at 10:46
  • Yes `--cov=folder_name` where `folder_name` is a folder that contains the files on which I want to get code coverage analysis. – variable May 26 '20 at 11:11
  • Hm, works without problems for me (pytest-5.4.2). Maybe these are files in subdirectories without an `__init__.py`? – MrBean Bremen May 26 '20 at 12:00
  • Please provide a [mcve]. The issue usually lies in early importing modules (before the coverage plugin was loaded). – hoefling May 26 '20 at 18:09
  • @MrBeanBremen - Yes it happens when there is no `__init__.py` file. What is the reason for this? – variable May 27 '20 at 08:14
  • 1
    Does this answer your question? [Coverage.py does not discover tests without init.py file in sub directories](https://stackoverflow.com/questions/47640812/coverage-py-does-not-discover-tests-without-init-py-file-in-sub-directories) – MrBean Bremen May 27 '20 at 08:51

1 Answers1

3

I have encountered the same issue.

After a quick search, I've found that you have to lay out __init__ structure in your source folder to be able to collect in the coverage report those files that are not being tested.

See this for more about __init__ files.

  • 1
    Good point, this is also commented on https://github.com/pytest-dev/pytest-cov/issues/88 – kevin Mar 03 '22 at 13:50
  • Still seeing source files not included as zero coverage if not exercised by test cases, even with a `__init__`. However since we are "blind" to the project, we have to use `--cov=.` with `PYTEST_ADDOPT` - which may have be insufficient? – simon.watts Mar 08 '22 at 12:52