Using omit flag in Python coverage.py API

三世轮回 提交于 2020-04-12 18:53:07

问题


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?


回答1:


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.




回答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__*'])



回答3:


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


来源:https://stackoverflow.com/questions/8392485/using-omit-flag-in-python-coverage-py-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!