Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using omit flag in Python coverage.py API

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?

like image 367
jsookiki Avatar asked Dec 05 '11 22:12

jsookiki


People also ask

Does coverage work with Pytest?

You can use Coverage.py with both unittest and Pytest.

What is Pragma no cover?

Coverage.py provides the feature “pragma: no cover” to exclude one or more lines of code from coverage reports. There are two main solutions to use this feature: based on code comments or based on configuration files. Figure 1(a) shows an example in which code is excluded via a code comment (i.e., #pragma: no cover).


2 Answers

Try omitting 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')
like image 148
Tgilgul Avatar answered Sep 21 '22 10:09

Tgilgul


create this .coveragerc file

# .coveragerc to control coverage.py
[run]
branch = True
omit =
        *Test*
        */usr/local/lib*
        */__init__.py


[report]
omit =
        *Test*
        */usr/local/lib*
        */__init__.py

# Regexes for lines to exclude from consideration
exclude_lines =
    # Have to re-enable the standard pragma
    pragma: no cover

    # Don't complain about missing debug-only code:
    def __repr__
    if self\.debug

    # Don't complain if tests don't hit defensive assertion code:
    raise AssertionError
    raise NotImplementedError

    # Don't complain if non-runnable code isn't run:
    if 0:
    if __name__ == .__main__.:

ignore_errors = True

[html]


directory = coverage_html_report
like image 45
Marcelo Fonseca Avatar answered Sep 18 '22 10:09

Marcelo Fonseca