1

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?](http://stackoverflow.com/questions/35513257/can-python-coverage-module-conditionally-ignore-lines-in-a-unit-test) – oblalex Jan 21 '17 at 17:00

3 Answers3

1

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".

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
0

Create .coveragerc and define rules for skipping lines during report generation:

[report]
exclude_lines =
    pragma: no cover
oblalex
  • 5,366
  • 2
  • 24
  • 25
  • But how do I specify to ignore a particular line if platform is windows otherwise ignore some other line? – Hitesh Ramchandani Jan 21 '17 at 11:39
  • You can create separate config files for different platforms. E.g.; `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). – oblalex Jan 21 '17 at 16:56
0

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/

xuhdev
  • 8,018
  • 2
  • 41
  • 69