views:

313

answers:

3

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?

A: 

Did you try:

coverage xml ae
Ned Batchelder
I get the following error: No source for code: '/home/matthew/dev/projects/trimurti/src/ae': [Errno 21] Is a directory: '/home/matthew/dev/projects/trimurti/src/ae'
Wraith
A: 

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')
Wraith
+1  A: 

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.

Awesome! Is there a way to do it without a full path?
Wraith
You don't have to give the full path, just a prefix (first part) of the absolute path. I don't think you can use relative paths, if that's what you're asking.