TIL: exclude_also with coverage.py

Sometimes you have code you want to exclude from the test coverage report, because it doesn’t really make sense to test it.

For example, maybe you want to exclude:

<span>if</span> <span>__name__</span> <span>==</span> <span>"</span><span>__main__</span><span>"</span><span>:</span>
<span>main</span><span>()</span>
<span>if</span> <span>__name__</span> <span>==</span> <span>"</span><span>__main__</span><span>"</span><span>:</span>
    <span>main</span><span>()</span>
if __name__ == "__main__": main()

Enter fullscreen mode Exit fullscreen mode

The old advice was to add something like this to .coveragerc:

<span>[report]</span>
<span># Regexes for lines to exclude from consideration </span><span>exclude_lines</span> <span>=</span>
<span># Have to re-enable the standard pragma: </span> <span>pragma:</span> <span>no</span> <span>cover</span>
<span># Don't complain if non-runnable code isn't run: </span> <span>if</span> <span>__name__</span> <span>=</span><span>= .__main__.:</span>
<span>[report]</span>
<span># Regexes for lines to exclude from consideration </span><span>exclude_lines</span> <span>=</span>
    <span># Have to re-enable the standard pragma: </span>    <span>pragma:</span> <span>no</span> <span>cover</span>

    <span># Don't complain if non-runnable code isn't run: </span>    <span>if</span> <span>__name__</span> <span>=</span><span>= .__main__.:</span>
[report] # Regexes for lines to exclude from consideration exclude_lines = # Have to re-enable the standard pragma: pragma: no cover # Don't complain if non-runnable code isn't run: if __name__ == .__main__.:

Enter fullscreen mode Exit fullscreen mode

But since coverage.py 7.2.0 (2023-02-22) you can use exclude_also instead and skip that pragma:

<span>[report]</span>
<span># Regexes for lines to exclude from consideration </span><span>exclude_also</span> <span>=</span>
<span># Don't complain if non-runnable code isn't run: </span> <span>if</span> <span>__name__</span> <span>=</span><span>= .__main__.:</span>
<span>[report]</span>
<span># Regexes for lines to exclude from consideration </span><span>exclude_also</span> <span>=</span>
    <span># Don't complain if non-runnable code isn't run: </span>    <span>if</span> <span>__name__</span> <span>=</span><span>= .__main__.:</span>
[report] # Regexes for lines to exclude from consideration exclude_also = # Don't complain if non-runnable code isn't run: if __name__ == .__main__.:

Enter fullscreen mode Exit fullscreen mode

Which is:

[report]
# Regexes for lines to exclude from consideration
<span>-exclude_lines = - # Have to re-enable the standard pragma: - pragma: no cover - </span><span>+exclude_also = </span> # Don't complain if non-runnable code isn't run:
if __name__ == .__main__.:
 [report]
 # Regexes for lines to exclude from consideration
<span>-exclude_lines = - # Have to re-enable the standard pragma: - pragma: no cover - </span><span>+exclude_also = </span>     # Don't complain if non-runnable code isn't run:
     if __name__ == .__main__.:
[report] # Regexes for lines to exclude from consideration -exclude_lines = - # Have to re-enable the standard pragma: - pragma: no cover - +exclude_also = # Don't complain if non-runnable code isn't run: if __name__ == .__main__.:

Enter fullscreen mode Exit fullscreen mode

Thanks

To Brian Okken for the tip.

To Ned Batchelder for maintaining Coverage.py.

To the Library of Congress and Flickr Commons for the photo of a covered wagon.

原文链接:TIL: exclude_also with coverage.py

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
In the face of difficulties, be brave, persistent and tirelessly to overcome it.
面对困难的时候,要勇敢、执着、不畏艰辛地去战胜它
评论 抢沙发

请登录后发表评论

    暂无评论内容