Python Coverage: testing a program

Ned Batchelder ned at nedbatchelder.com
Thu Oct 24 19:53:44 EDT 2013


On 10/24/13 6:28 PM, Ethan Furman wrote:
> On 10/24/2013 01:54 PM, Ben Finney wrote:
>> Terry Reedy <tjreedy at udel.edu> writes:
>>
>>> On 10/24/2013 1:46 PM, Ned Batchelder wrote:
>>>> It's been fun dropping the contortions for coverage.py 4.x, though!
>>>
>>> One request: ignore "if __name__ == '__main__':" clauses at the end of
>>> files, which cannot be run under coverage.py, so 100% coverage is
>>> reported as 100% instead of 9x%.
>>
>> You can do this already with current Coverage: tell Coverage to exclude
>> <URL:http://nedbatchelder.com/code/coverage/excluding.html> specific
>> statements, and it won't count them for coverage calculations.
>
> While that's neat (being able to exclude items) is there any reason to 
> ever count the `if __name__ == '__main__'` clause?  Are there any 
> circumstances where it could run under Coverage? (Apologies if this is 
> a dumb question, I know nothing about Coverage myself -- but I'm going 
> to go look it up now.  ;)
>

Sure, if that line appears in program.py, then it will be run if you 
execute program.py:  $ coverage run program.py

You can run coverage a number of times, even with different main 
programs, then combine all the data to produce a combined report. This 
way you could cover all of the __main__ clauses in a number of files.

--Ned.

> -- 
> ~Ethan~
>
>




More information about the Python-list mailing list