Ignore ABC abstract methods
See original GitHub issueIs your feature request related to a problem? Please describe. Sometimes you want to create an abstract class with abstract methods that will be overridden by other subclasses. For example:
import abc
class Foo(abc.ABC):
@abc.abstractmethod
def a():
...
class Bar(Foo):
def a():
return 1
Pay attention that while Bar.a
content should be covered by test, the content of Foo.a
should not since there is no actual gain in covering it with a test.
Describe the solution you’d like Coverage should ignore abstract methods by default and should not require coverage for them.
Describe alternatives you’ve considered
If you think that sometimes abstract methods should be covered, maybe we can make this ability toggleable by a flag (--ignore-abstract
) for example.
Additional context This API is available only in Python 3, therefore we should make sure we disable it in Python 2.
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (8 by maintainers)
There’s something people are missing in this discussion: since coverage.py v4.1, if you exclude a decorator, it excludes the entire decorated function. So the best exclude line here would be:
Once you do that, the entire abstract method is excluded from coverage, no matter what statements it contains.
BTW, this is also being discussed in pytest-cov: https://github.com/pytest-dev/pytest-cov/issues/428
You don’t want to add “…”, that’s a regex that matches almost every line of your code (se #1042). Try the setting I suggested, I think it will work for you.
I’m reluctant to add hard-coded options for language features, because different people approach them differently, and the language changes over time.