Last updated on December 20, 2020
When testing Python programs, coverage.py is often used in measuring code coverage, and enforcing 100% code coverage is regarded as a good practice:
# .coveragerc
[coverage:report]
# Enforce 100% coverage test
fail_under = 100
show_missing = True
However, if there are some lines of code that are platform dependent (i.e., they are never executed on at least one platform), code coverage tests usually fail. For example, the following code snippet would always lead to a coverage that is less than 100% on a platform other than Windows:
if os.name != 'nt':
# Do something if the OS is not Windows...
You can ask coverage.py to ignore this block by adding a comment # pragma: no cover
, but then coverage.py would ignore it on all platforms, including all non-Windows platforms. If you use tox for testing, this issue can be resolved cleanly.
Continue reading →