Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 506
JPG Avatar asked Oct 30 '20 15:10

JPG


People also ask

How do you exclude a function from code coverage in Python?

You can exclude them all at once without littering your code with exclusion pragmas. If the matched line introduces a block, the entire block is excluded from reporting. Matching a def line or decorator line will exclude an entire function.

How do you exclude a class from code coverage?

Edit Configurations > Select Code Coverage tab > then adding the package or class I want to be excluded or include only in the code coverage report.

How do you ignore code coverage?

The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.

How do I exclude a folder from test coverage?

You'll have to specify in omit **/tests/* to ignore nested test folders.


1 Answers

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.

like image 99
JPG Avatar answered Oct 05 '22 07:10

JPG