Pytest-Cov Show Coverage For Untouched Files
Asked Answered
B

1

9

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?

Banjermasin answered 7/4, 2022 at 9:14 Comment(2)
Oh wow, I came here using Google top result having the same question... surprised to see zero upvotes and answers. 🤔Delladelle
Duplicate of #62018620Frambesia
D
9

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
[...]
Delladelle answered 10/6, 2022 at 20:37 Comment(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.Frambesia

© 2022 - 2024 — McMap. All rights reserved.