pytest-dev / pytest-cov

Coverage plugin for pytest.
MIT License
1.72k stars 211 forks source link

Why can't coverage the data in the "if __name__ == "__main__":" #552

Closed zhuofeng6 closed 1 year ago

zhuofeng6 commented 2 years ago

test.py

if __name__ == "__main__":
    print("hello world!")

run:pytest --cov=./ test.py

output:

----------- coverage: platform linux, python 3.9.9-final-0 -----------
Name      Stmts   Miss  Cover
-----------------------------
test.py       2      1    50%
-----------------------------
TOTAL         2      1    50%

The normal coverage rate should be 100%. why 50%.

image

zhuofeng6 commented 2 years ago

nobody respond? it is a bug?

Segelzwerg commented 1 year ago

I think we experienced the same thing. The coverage was lost from 3.0.0 to 4.0.0.

https://github.com/Whist-Team/Whist-Core/pull/247 https://github.com/Whist-Team/Whist-Server/pull/359

ionelmc commented 1 year ago

The test file is imported, not run as a script thus that if __name__ == "__main__" branch can't be covered. It's not like it is run anyway. You're complaining that dead code isn't run.

ionelmc commented 1 year ago

@Segelzwerg you're using multiprocessing in your test suite, support for it was removed in pytest-cov 4. See the changelog for migration path: https://pytest-cov.readthedocs.io/en/latest/changelog.html

Segelzwerg commented 1 year ago

@Segelzwerg you're using multiprocessing in your test suite, support for it was removed in pytest-cov 4. See the changelog for migration path: https://pytest-cov.readthedocs.io/en/latest/changelog.html

@ionelmc thanks for the advice. I didn't realize it. Will investigate now!