Some part of code works on Windows and some part works on other platforms. I want to increase the coverage of the code by placing #pragma: no cover appropriately. So when the program is running on Windows platform, the code related to other platforms should be ignored and vice versa. How can I achieve this?
-
Possible duplicate of Can python coverage module conditionally ignore lines in a unit test?o3bvv– o3bvv2017-01-21 17:00:38 +00:00Commented Jan 21, 2017 at 17:00
4 Answers
Create .coveragerc and define rules for skipping lines during report generation:
[report]
exclude_lines =
pragma: no cover
2 Comments
pragma: no cover linux and pragma: no cover win32, etc. Then pass those configs to coverage manually or select them automatically depending on target platform, e.g. inside setup.py or inside your test suit runner (which you may have to create).A better solution is to not ignore the lines at all, and instead to measure the coverage on all the platforms, and then combine them together.
You can run coverage in "parallel mode" so that each data file gets a distinct name, with parallel=true. Then copy all the data files to one place, run "coverage combine", and then "coverage report".
Comments
There's one solution if you use tox.
First, add the following to tox.ini:
# tox.ini
[tox]
envlist = py{36,37,38,39}-{linux,macos,windows}
[testenv]
platform = linux: linux
macos: darwin
windows: win32
setenv =
linux: PLATFORM = linux
macos: PLATFORM = macos
windows: PLATFORM = windows
[testenv:py{36,37,38,39}-{linux,macos,windows}]
setenv =
COVERAGE_RCFILE = {envtmpdir}/coveragerc
commands_pre =
{envpython} -c 'from pathlib import Path; Path(r"{env:COVERAGE_RCFILE}").write_text(Path(".coveragerc.in").read_text().format(platform="{env:PLATFORM}"))'
commands =
coverage run -m pytest -v
The snippet above makes tox platform-aware and create a coveragerc file that is recognized by Coverage.py.
Then, add the following to .coveragerc.in:
[coverage:report]
exclude_lines =
pragma: no cover {platform}
This line sets the exclusion line based on platform. Now, mark your lines of code with # pragma: no cover windows, # pragma: no cover linux, or # pragma: no cover macos to exclude those lines from execution on a specific platform.
Now running tox -e py39-windows (or other Python versions and Oses, e.g., tox -e py38-linux) will measure the coverage based on the platform.
Reference: https://www.topbug.net/blog/2020/12/19/platform-dependent-python-coverage-test-with-tox/
Comments
Seems that you can do this pretty easily with the coverage-conditional-plugin.
Installing
pip install coverage-conditional-plugin
Then add the tool in your pyproject.toml (at the root of your repository):
[tool.coverage.run]
plugins = ["coverage_conditional_plugin"]
Skipping full files (modules)
Done in configuration "omit" section. In pyproject.toml:
[tool.coverage.coverage_conditional_plugin.omit]
# Include some/windows_specific/*.py only on Windows
"platform_system != 'Windows'" = "some/windows_specific/*.py"
# Skip "some/not_for_windows/*.py" on Windows
"platform_system == 'Windows'" = "some/not_for_windows/*.py"
Skipping lines or blocks of code
Done with # pragma some-comment and configuration. In pyproject.toml:
[tool.coverage.coverage_conditional_plugin.rules]
no-cover-if-windows = "platform_system == 'Windows'"
no-cover-if-not-windows = "platform_system != 'Windows'"
and then in code:
if ON_WINDOWS: # pragma: no-cover-if-not-windows
do_something_only_on_windows()
else: # pragma: no-cover-if-windows
do_something_on_other_systems()