如何为单个包生成覆盖率 xml 报告?

发布于 2024-08-21 23:05:12 字数 1025 浏览 6 评论 0原文

我使用nose覆盖率来生成覆盖率报告。我现在只有一个包,ae,所以我指定只覆盖它:

nosetests -w tests/unit --with-xunit --with-coverage --cover-package=ae

以下是结果,看起来不错:

Name             Stmts   Exec  Cover   Missing
----------------------------------------------
ae                   1      1   100%   
ae.util            253    224    88%   39, 63-65, 284, 287, 362, 406
----------------------------------------------
TOTAL              263    234    88%   
----------------------------------------------------------------------
Ran 68 tests in 5.292s

但是,当我运行 coverage xml 时,覆盖率拉入不必要的包,包括与我的代码无关的 python emaillogging 包。

如果我运行 coverage xml ae,我会收到此错误:

No source for code: '/home/wraith/dev/projects/trimurti/src/ae': 
[Errno 21] Is a directory: '/home/wraith/dev/projects/trimurti/src/ae'

Is there a way to generated the XML for just the ae package?

I'm using nose and coverage to generate coverage reports. I only have one package right now, ae, so I specify to only cover that:

nosetests -w tests/unit --with-xunit --with-coverage --cover-package=ae

And here are the results, which look good:

Name             Stmts   Exec  Cover   Missing
----------------------------------------------
ae                   1      1   100%   
ae.util            253    224    88%   39, 63-65, 284, 287, 362, 406
----------------------------------------------
TOTAL              263    234    88%   
----------------------------------------------------------------------
Ran 68 tests in 5.292s

However when I run coverage xml, coverage pulls in more packages than necessary, including python email and logging packages which have nothing to do with my code.

If I run coverage xml ae, I get this error:

No source for code: '/home/wraith/dev/projects/trimurti/src/ae': 
[Errno 21] Is a directory: '/home/wraith/dev/projects/trimurti/src/ae'

Is there a way to generate the XML for just the ae package?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅气尐潴 2024-08-28 23:05:12

我遇到了类似的问题并使用 --omit 选项解决了它。这使其运行速度更快,并将coverage.xml 的大小从2MB 减少到70kB。

--omit=PRE1,PRE2,...  Omit files when their filename path starts with one of
                      these prefixes.

我使用的是 Mac OS X,因此省略了 /Library/ 和 /Applications/ 文件夹:

$ coverage xml --omit=/Library/,/Applications/

在其他系统上,您可能会发现 --omit=/usr/ 更有帮助。

I had a similar problem and solved it with the --omit option. This made it run much faster and reduced the size of coverage.xml from 2MB to 70kB.

--omit=PRE1,PRE2,...  Omit files when their filename path starts with one of
                      these prefixes.

I'm on Mac OS X, so I omitted the /Library/ and /Applications/ folders:

$ coverage xml --omit=/Library/,/Applications/

On other systems, you may find --omit=/usr/ more helpful.

貪欢 2024-08-28 23:05:12

你尝试过吗:

coverage xml ae

Did you try:

coverage xml ae
浅语花开 2024-08-28 23:05:12

我无法找到这个问题的答案,所以我在处理后剥离不需要的包元素。此函数采用原始 XML 文件、要检查的元素名称、要检查的属性、要保留的模式(或单词列表)以及新文件的目标文件路径。

from lxml import etree

def keep(self, xmlfile, elem_name, attr_name, pattern, dst):
    try: 
        rep = re.compile(pattern)
    except TypeError:
        # Create regex pattern if a list is given. 
        # TypeError: unhashable type: 'list'
        rep = re.compile("|".join(pattern))

    dom = etree.parse(xmlfile)
    for node in dom.findall('//%s' % elem_name):
        if not rep.search(node.get(attr_name)):
            node.getparent().remove(node)

    dom.write(dst)

为了解决我的问题,我这样称呼它:

keep('coverage.xml', 'package', 'name', 'ae|tests', 'wanted-coverage.xml')

I wasn't able to find the answer to this, so I'm stripping the unwanted package elements out after processing. This function takes the original XML file, the element name to check, its attribute to check, the pattern (or list of words) you'd like to KEEP, and a destination filepath for the new file.

from lxml import etree

def keep(self, xmlfile, elem_name, attr_name, pattern, dst):
    try: 
        rep = re.compile(pattern)
    except TypeError:
        # Create regex pattern if a list is given. 
        # TypeError: unhashable type: 'list'
        rep = re.compile("|".join(pattern))

    dom = etree.parse(xmlfile)
    for node in dom.findall('//%s' % elem_name):
        if not rep.search(node.get(attr_name)):
            node.getparent().remove(node)

    dom.write(dst)

To solve my problem, I'm calling it like this:

keep('coverage.xml', 'package', 'name', 'ae|tests', 'wanted-coverage.xml')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文