跳转到主要内容

一个通过HTML可视化覆盖率报告的脚本

项目描述

https://travis-ci.org/zopefoundation/z3c.coverage.png

此包包含用于处理Python覆盖率数据的工具。

coveragereport可以从覆盖率数据生成HTML报告,包括带语法高亮的源代码和每个包的汇总数据。

coveragediff比较两组覆盖率报告,并报告回归,即未测试代码行数的增加。

使用coveragereport

$ coveragereport --help
Usage: coveragereport [options] [inputpath [outputdir]]

Converts coverage reports to HTML.  If the input path is omitted, it defaults
to coverage or .coverage, whichever exists.  If the output directory is
omitted, it defaults to inputpath + /report or ./coverage-reports, depending
on whether the input path points to a directory or a file.

Options:
  -h, --help            show this help message and exit
  -q, --quiet           be quiet
  -v, --verbose         be verbose (default)
  --strip-prefix=PREFIX
                        strip base directory from filenames loaded from
                        .coverage
  --path-alias=PATH=LOCALPATH
                        define path mappings for filenames loaded from
                        .coverage

使用zope.testrunner的示例

$ bin/test --coverage=coverage
$ coveragereport
$ ln -s mypackage.html coverage/report/index.html
$ xdg-open coverage/report/index.html
$ xdg-open coverage/report/all.html

使用nosecoverage.py的示例

$ nosetests --with-coverage --cover-erase
$ coveragereport --strip-prefix=/full/path/to/source/
$ ln -s mypackage.html coverage-reports/index.html
$ xdg-open coverage-reports/index.html
$ xdg-open coverage-reports/all.html

示例报告

http://i.imgur.com/mkqA3.png

使用coveragediff

Usage: coveragediff [options] olddir newdir

Options:
  -h, --help         show this help message and exit
  --include=REGEX    only consider files matching REGEX
  --exclude=REGEX    ignore files matching REGEX
  --email=ADDR       send the report to a given email address (only if
                     regressions were found)
  --from=ADDR        set the email sender address
  --subject=SUBJECT  set the email subject
  --web-url=BASEURL  include hyperlinks to HTML-ized coverage reports at a
                     given URL

使用zope.testrunner的示例

$ bin/test --coverage=coverage
$ vi src/...
$ mv coverage coverage.old
$ bin/test --coverage=coverage
$ coveragediff coverage.old coverage

您不能使用coveragediffcoverage.py数据。下面将详细介绍。

输出示例

$ coveragediff coverage.old coverage
my.package.module: 36 new lines of untested code
my.package.newmodule: new file with 15 lines of untested code (out of 23)

带有可点击链接的输出

$ coveragediff coverage.old coverage --web-url=http://example.com/coverage
my.package.module: 36 new lines of untested code
See http://example.com/coverage/my.package.module.html

my.package.newmodule: new file with 15 lines of untested code (out of 23)
See http://example.com/coverage/my.package.newmodule.html

通过电子邮件输出,方便持续集成

$ coveragediff coverage.old coverage --web-url=http://example.com/coverage \
                   --email 'Developers <dev@exmaple.com>' \
                   --from 'Buildbot <buildbot@example.com>'

最后一个例子不会产生任何输出,而是发送一封电子邮件(通过SMTP到localhost:25)。

获取覆盖率数据

zope.testrunner

zope.testrunner可以生成一个包含名为dotted.package.name.cover的文件的目录,这些文件包含带有覆盖率信息的源代码。要获取这些文件,请使用

bin/test --coverage=outdir/

both coveragereportcoveragediff 都接受此作为输入。

coverage.py

coverage.py可以生成一个包含(不完整)覆盖率信息的.coverage文件。要获取它,请使用

coverage run bin/testrunner

coveragereport可以接受.coverage文件作为输入,但它还需要访问匹配的源文件。您必须手动指定源树的绝对路径前缀,以便报告知道如何将文件名转换为点分包名。此外,仅使用绝对路径名是不够的,您需要提供规范的绝对路径名(没有符号链接段),例如由os.path.realpath返回的路径。这非常不方便。抱歉。

coveragediff无法比较两个.coverage文件并报告回归。原因之一是数据格式的不完整性(它包含执行语句的行号,但没有说明哪些行包含代码,哪些行是空白/注释/续行符/排除的源行)。另一个原因更简单:没有人编写代码。;)

不幸的是,coverage annotate不会生成与coveragereport/coveragediff兼容的文件。如果有人编写了一个补丁,这也可以得到解决。

trace.py

zope.testrunner产生的*.cover标注源格式实际上来自Python标准库模块trace.py。您可能可以直接使用trace.py。我从未尝试过。

常见问题解答

为什么使用z3c.coverage而不是coverage html?

有些人更喜欢z3c.coverage生成的报告的外观。有些人发现按包的覆盖率摘要或树状导航方便。

我应该使用zope.testrunner内置的覆盖率,还是使用coverage run bin/test?

coverage.py要快得多,但使用它(并将其连接到z3c.coverage)可能不太方便。例如,如果您使用zc.buildout 1.5.xzc.recipe.testrunner,您将无法使用coverage run bin/test,因为前者有神秘的半损坏的站点隔离魔法。

有人真的提出过这些问题吗?

自己提问算吗?

变更

2.1.0 (2017-04-24)

  • 支持(并要求)coverage.py >= 4.0。

  • 修复处理coverage.py数据时错误突出显示未访问行的问题(自2.0.2以来已损坏)。

  • 停止支持Python 2.6。

  • 增加了对Python 3.4、3.5和3.6的支持。

2.0.3 (2015-11-09)

  • 标准化命名空间__init__。

2.0.2 (2013-10-01)

  • 错误:coveragereport现在也接受源文件中的非ASCII字符(同时修复了enscript不可用的情况)。

2.0.1 (2013-10-01)

  • 错误:coveragereport现在也接受源文件中的非ASCII字符。

2.0.0 (2013-02-20)

  • 增加了对Python 3.3和PyPy 1.9的支持。

  • 停止支持Python 2.4和2.5。

1.3.1 (2012-10-24)

  • 更好的PyPI描述。Doctests是测试,而不是文档。

  • coveragereport 现在接受 --path-alias 参数。

  • coveragereport:在黄色(90%)和绿色(100%)之间增加了一个新颜色级别,即黄绿色(95%)。

1.3.0 (2012-09-06)

  • coveragereport 现在接受 --help--verbose--quiet 参数,默认开启详细模式。

  • coveragereport 现在可以处理由 http://pypi.python.org/pypi/coverage 生成的 .coverage 文件。

  • 错误修正:在 all.html 报告中,按未覆盖行数排序出现问题。

1.2.0 (2010-02-11)

  • coverage 脚本重命名为 coveragereport,以避免与 Ned Batchelder 的优秀 coverage.py 冲突。

1.1.3 (2009-07-24)

  • 错误:Doctest 没有在 coveragediff.txt 中标准化空白字符。尽管在独立测试时通过,但在运行所有测试时失败了。

1.1.2 (2008-04-14)

  • 错误:当包路径中包含“test”一词时,它被忽略在覆盖率报告中。然而,预期的行为是忽略与设置测试相关的文件。

  • 错误:在 README.txt 中对 os.listdir() 的结果进行排序,以避免非确定性失败。

  • 错误:忽略单元测试和功能测试模块的逻辑也忽略了称为 testing 的模块和包。

  • 将标题中的“单元测试覆盖率”更改为“测试覆盖率”——它对功能测试也适用。

1.1.1 (2008-01-31)

  • 错误:当发布包时,测试 SVN 版本号的可用性的测试失败。使测试更可靠。

1.1.0 (2008-01-29)

  • 特性:主覆盖率报告函数 main() 现在接受脚本参数作为函数参数,这使得从 buildout 配置脚本更容易。

  • 特性:如果报告目录不存在,报告生成器会为您创建它。

  • 特性:通过创建可以生成覆盖率报告的 buildout 来食用自己的狗粮。

  • 错误:提高了测试覆盖率到 100%。

1.0.1 (2007-09-26)

  • 错误:修复了元数据。

1.0.0 (2007-09-26)

  • 首次公开发布。

0.2.1

  • 特性:在 coveragediff 中添加了 --web 选项。

  • 特性:添加了测试套件。

0.2.0

  • 特性:添加了 coveragediff.py

0.1.0

  • 发布 coveragereport.py 的初始版本。

下载文件

下载适合您平台的文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。

源代码分发

z3c.coverage-2.1.0.tar.gz (46.3 kB 查看哈希值)

上传时间 源代码

支持