question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ionelmccommented, Oct 3, 2022

@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

0reactions
Segelzwergcommented, Oct 3, 2022

@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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test or mock "if __name__ == '__main__'" contents
Heya, I've added a new answer that gives 100% test coverage (with tests !) and doesn't require ignoring anything. Let me know what...
Read more >
What Does if __name__ == "__main__" Do in Python?
When you import your file as a module, the code that you nested under if __name__ == "__main__" doesn't execute. Now that you...
Read more >
How to test if __name__ == '__main__' - Medium
How to test if __name__ == '__main__'. You need to test those two pesky lines at the end of your app, if you...
Read more >
What does if __name__ == "__main__" do? - Python Engineer
When the Python interpreter reads a source file, it does two things: First, it sets a few special variables like __name__; Then it...
Read more >
Python Tutorial: if __name__ == '__main__' - YouTube
... in Python: if __name__ == '__main__ ':This conditional is used to check whether a python m... ... Your browser can't play this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found