Search code examples
pythoncoverage.pytest-coverage

Exclude a function from coverage


I am using coverage.py to get the test coverage of the code.

Suppose I have two functions with the same name in two different modules

# foo/foo.py

def get_something():
    # fetch something
    # 10 line of branch code
    return "something foo/foo.py"


# bar/foo.py

def get_something():
    # fetch something
    # 20 line of branch code
    return "something bar/foo.py"

How can I exclude the bar.foo.get_something(...) function "completely" ?


Solution

  • We can use pragma comment on the function definition level which tells the coveragepy to exclude the function completely.

    # bar/foo.py
    
    def get_something():  # pragma: no cover
        # fetch something
        # 20 line of branch code
        return "something bar/foo.py"

    Note

    If we have the coveragepy config file with an exclude_lines setting in it, make sure that pragma: no cover in that setting because it overrides the default.