Using omit flag in Python coverage.py API

jsookiki picture jsookiki · Dec 5, 2011 · Viewed 14.4k times · Source

I'm using the python coverage.py to create a very basic test suite with coverage. Currently everything works great. However, my coverage report includes all the /usr/local/lib libraries that are called and all the __init__.py files.

Here's what my coverage report call looks like right now:

self.cov.html_report(directory='coverage', omit='*Test*, */usr/local/lib*,*__init__*')

The goal is to use the omit flag to remove all classes with the word "Test", "/usr/local/lib", or "__init__" in them. Since I can't find too much on the web about this in the API (There's plenty about how to do it on the command line), does someone know what the correct syntax to make this work would be?

Answer

Tgilgul picture Tgilgul · Mar 11, 2015

Try ommiting unwanted files in the coverage() call:

self.cov = coverage.coverage(omit=['*Test*', '*/usr/local/lib*','*__init__*'])

I would recommand using the coverage config file (default is .coveragerc):

# .coveragerc to control coverage.py

[run]
omit =
        *__init__*
        */usr/local/lib*
        *Test*

[html]
omit =
        *__init__*
        */usr/local/lib*
        *Test*

The coverage call takes the .coveragerc file into consideration by default, but if you want to make sure use:

self.cov = coverage.coverage(config_file=True)

Alternatively, you can change the config file name and pass it as an argument:

self.cov = coverage.coverage(config_file='/your/path/.coverage_config_file')

Hope this helps.