13

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?

3 Answers 3

23

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')
Sign up to request clarification or add additional context in comments.

2 Comments

Apparently now the html block only allows options that specifically target the HTML report output. In order to omit certain files in a report, the omit option must be defined in the [report] block. See coverage.readthedocs.io/en/coverage-4.0.3/config.html
Also apparently the spaces are important, you can't have different omits all starting on the first line
6

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

Comments

2

From the docs at http://nedbatchelder.com/code/coverage/api.html#api

include and omit are lists of filename patterns. Files that match include will be measured, files that match omit will not. Each will also accept a single string argument.

So try it like...

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.