[TIP] coverage - conditionally exclude code?

Albert-Jan Roskam fomcl at yahoo.com
Fri Jun 5 12:47:39 PDT 2015



----- Original Message -----

> From: Ned Batchelder <ned at nedbatchelder.com>
> To: testing-in-python at lists.idyll.org
> Cc: 
> Sent: Friday, June 5, 2015 7:39 PM
> Subject: Re: [TIP] coverage - conditionally exclude code?
> 
> 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.



Aah, I see. Yes, Andre Caron's email ('Getting combined coverage with Tox', earlier today) 

already made me realize that using 'combine' was the way to go. I will definitely try this.
It's going to be challenging to get the Windows coverage reports from Appveyor to my own Linux 

machine, I guess.


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


I'll keep this in mind. I only have a few OS-specific functions (e.g. ctypes.wintypes stuff),
so at least the code won't be littered with 'pragma' comments.

Thanks for your fantastic package Ned!

Albert-Jan



More information about the testing-in-python mailing list