[TIP] coverage - conditionally exclude code?
Ned Batchelder
ned at nedbatchelder.com
Fri Jun 5 10:39:24 PDT 2015
On 6/5/15 10:59 AM, Albert-Jan Roskam wrote:
>
> Hello,
>
> Is it possible to conditionally exclude bits of code from coverage measurements?
> For example:
> -under Python 2, can I exclude Python-3-only pieces of code?
> -under Linux, can I exclude Windows-specific functions?
>
>
> I have been looking here, but it does not appear to be possible:
>
> http://nedbatchelder.com/code/coverage/excluding.html#excluding
> The only way I can think of is by somehow dynamically adjusting the exclude_lines in .coveragerc
The first thing to consider is not doing version-specific exclusion at
all. Run your tests under all your scenarios, combine the coverage
results, and produce a coverage report. Doing this, you don't need to
exclude anything for specific versions.
If you really do want version-specific exclusions: The best way we've
come up with is to use a number of .coveragerc files. Each can define
"[report] exclude_lines" slightly differently. So .coveragerc_py3 could
have an exclude_lines with "# pragma: no cover" and "# pragma: no py3",
etc. Then you can use "# pragma: no py3" to exclude lines that won't be
run under Python 3.
--Ned.
More information about the testing-in-python
mailing list