5

When I run my tests, I realised that pytest-cov only shows coverage reports for files that are touched during the automated tests. How can I set it so that it shows coverage for even files that are not touched?

Daniel
  • 67
  • 1
  • 7
  • Oh wow, I came here using Google top result having the same question... surprised to see zero upvotes and answers. – gertvdijk Jun 10 '22 at 20:31
  • Duplicate of https://stackoverflow.com/questions/62018120/pytest-cov-does-not-consider-a-file-for-coverage-analysis-unless-it-is-imported – Stefan Dec 22 '22 at 14:35

1 Answers1

5

I had the exact same issue. Make sure you have a __init__.py file in every subdirectory (package).

More info: why you want __init__.py files.

My pyproject.toml file for reference:

[tool.pytest.ini_options]
addopts = [
    "--cov=mypackage",
    "--cov-report=term-missing",
]

output:

Name                                           Stmts   Miss  Cover   Missing
----------------------------------------------------------------------------
src/mypackage/__init__.py                          4      0   100%
src/mypackage/sub/__init__.py                      0      0   100%
src/mypackage/sub/mod1.py                         23     23     0%   1-33
src/mypackage/sub/mod2.py                         41     41     0%   1-61
[...]
gertvdijk
  • 24,056
  • 6
  • 41
  • 67
  • 1
    Thank you. It works. However, I would prefer a solution without __init__.py files. Only adding them for the purpose of test coverage seems to be weird. – Stefan Dec 22 '22 at 14:33