如何为单个程序包生成coverage XML报告? [英] How do I generate coverage xml report for a single package?

查看:232
本文介绍了如何为单个程序包生成coverage XML报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是,当我运行coverage xml时,coverage引入了比必要更多的软件包,其中包括与我的代码无关的python email logging 软件包.

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.

如果运行coverage xml ae,则会出现此错误:

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'

是否有一种仅为 ae 包生成XML的方法?

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

推荐答案

我找不到答案,所以我在处理后将不需要的包元素剥离了.此函数采用原始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')

这篇关于如何为单个程序包生成coverage XML报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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