How do I generate coverage xml report for a single

2020-07-20 00:00发布

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?

3条回答
神经病院院长
2楼-- · 2020-07-20 00:17

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.

查看更多
别忘想泡老子
3楼-- · 2020-07-20 00:26

Did you try:

coverage xml ae
查看更多
ら.Afraid
4楼-- · 2020-07-20 00:33

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')
查看更多
登录 后发表回答