21

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" ?

JPG
  • 82,442
  • 19
  • 127
  • 206

1 Answers1

30

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.

Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
JPG
  • 82,442
  • 19
  • 127
  • 206
  • Note that this does _not_ work on code blocks in general, e.g. `if` or `with cm:` etc. – Czechnology Dec 16 '22 at 15:06
  • @Czechnology I don't think so, [see this](https://imgur.com/a/qr7X2Eo) – JPG Dec 16 '22 at 17:08
  • from the [doc](https://coverage.readthedocs.io/en/latest/excluding.html#advanced-exclusion) *"The regexes only have to match part of a line. Be careful not to over-match. A value of `...` will match any line with more than three characters in it."* – JPG Dec 16 '22 at 17:10