python coverage模块能否有条件地忽略单元测试中的行? [英] Can python coverage module conditionally ignore lines in a unit test?

查看:391
本文介绍了python coverage模块能否有条件地忽略单元测试中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用鼻子测试和coverage模块,我希望代码的覆盖率报告能够反映出正在测试的版本。考虑以下代码:

Using nosetests and the coverage module, I would like coverage reports for code to reflect the version being tested. Consider this code:

import sys
if sys.version_info < (3,3):
    print('older version of python')

当我如果在python 3.5版中进行测试,则 print()显示为未经测试。我想让覆盖范围忽略该行,但仅当我使用python版本3.3+进行测试时

When I test in python version 3.5, the print() shows up as untested. I'd like to have coverage ignore that line, but only when I'm testing using python version 3.3+

是否可以执行类似#编译指示:仅当 sys.version_info print()语句上没有封底 c>不小于(3,3)?实际上,我想执行以下操作:

Is there a way to do something like # pragma: no cover on the print() statement only for when sys.version_info is not less than (3,3)? Effectively, I'd like to do something like this:

import sys
if sys.version_info < (3,3):
    print('older version of python') # pragma: [py26,py27,py32] no cover


推荐答案

正如您在评论中解释的那样,您担心的是,覆盖率报告仅显示行号,并且您希望避免重新输入

As you explain in the comments, your concern is, that the coverage report will only show line numbers, and you want to avoid having to re-check these again and again.

另一方面,我不太赞成使用带注释的混乱代码使一个或另一个工具满意:对我来说,这一切降低了可读性。因此,我想提出另一种方法,该方法可以避免代码混乱,但仍可以减轻您的负担,可以始终进行重新检查。

On the other hand, I am not much in favor of cluttering code with comments to make one or the other tool happy: To me all this is degrading readability. Thus, I'd like to propose another approach, that avoids cluttering the code, but still take away the burden from you to do that re-checking all the time.

这个想法是创建覆盖情况的基线,您可以将基线与未来的覆盖范围分析结果进行比较。例如,coverage.py的覆盖率报告如下所示(引自 http ://coverage.readthedocs.org/en/coverage-4.0.3/index.html ):

The idea is, to create a baseline of the coverage situation, against which you can compare future coverage analysis results. For example, the coverage report from coverage.py looks as follows (cited from http://coverage.readthedocs.org/en/coverage-4.0.3/index.html):

Name                      Stmts   Miss  Cover   Missing
-------------------------------------------------------
my_program.py                20      4    80%   33-35, 39
my_other_module.py           56      6    89%   17-23
-------------------------------------------------------
TOTAL                        76     10    87%

此输出可以用作以下内容的基础一个基准:一个粗略的想法(有关改进请参见下文)是,将输出存储为可接受的承保范围情况,并将其与将来的承保范围报告进行比较。不幸的是,每当行号更改时,您在比较报表时都会看到差异。为避免这种情况,可以改进此基本思想:

This output could be used as the basis for a 'baseline': The rough idea (for improvements see below) is, that you store this output as the 'accepted' coverage situation, and diff it against future coverage reports. Unfortunately, whenever line numbers change, you will see differences when diffing the reports. To avoid this, this basic idea can be improved:

借助简单的脚本,您可以转换报表,使报表的内容代替行号,而不是行号。显示了尊重线。例如,基于您上面的代码示例的假设报告可能看起来像这样:

With the help of simple scripting, you could transform the report such that instead of the line numbers, the contents of the respectice lines are shown. For example, a hypothetical report based on your code example above could look like:

Name                      Stmts   Miss  Cover   Missing
-------------------------------------------------------
my_program.py                20      1     5%   3

从此报告中,您可以为版本大于等于3.3的python版本创建以下覆盖基线,例如在文件 coverage-baseline-33andabove.txt 中:

From this report, you could create the following 'coverage baseline' for python versions >= 3.3, for example in file coverage-baseline-33andabove.txt:

my_program.py:
-    print('older version of python')

即使您在文件顶部添加了其他导入行,该基准线也看起来相同。

This baseline would look the same even if you add, for example, further import lines to the top of your file. Further baseline files would be created for the other python versions against which you determine the coverage.

进一步的基准文件将为您确定覆盖范围的其他python版本创建。进一步的改进可能是将几行行分开,例如:

Further possible improvements could be to separate groups of lines, like:

my_program.py:
*
-    print('older version of python')
*
-    cleanup()
-    assert False
my_program2.py:
*
-    print('older version of python')

仅当未覆盖的代码发生更改(添加,删除,修改,移动)以及文件名更改时,您才会看到差异。然后,出现差异将需要您存储新的覆盖范围基准,或者添加更多测试,直到再次达到原始基准内容。

You would only see differences whenever the non-covered code changes (additions, deletions, modifications, moves) and also when the file names change. Then, the occurrence of differences will require you to store a new 'coverage-baseline', or alternatively, add more tests until the original baseline content is reached again.

这篇关于python coverage模块能否有条件地忽略单元测试中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆