How do I generate coverage xml report for a single package?

断了今生、忘了曾经 提交于 2019-12-22 10:54:45

问题


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?


回答1:


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.




回答2:


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')



回答3:


Did you try:

coverage xml ae


来源:https://stackoverflow.com/questions/2293647/how-do-i-generate-coverage-xml-report-for-a-single-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!