question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Do not report abstract methods as missing coverage

See original GitHub issue

Summary

This feature requests especially concerns the term-missing report. By their nature, methods decorated with abc.abstractmethod cannot be called directly.

In the example below, pytest-cov would consider pass to be an uncovered statement.

@abc.abstractmethod
def some_abstract_method():
    pass

In this situation, it’s impossible to reach 100% coverage. I acknowledge that it’s possible to use the raise NotImplementedError approach instead of @abc.abstractmethod. However, I personally prefer to make use of abstract methods.

I would therefore greatly appreciate a feature that allows pytest-cov to omit abstract methods.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:22
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
coulingcommented, Apr 16, 2021

For future readers, I’ve just realised this can easily by done by adding pass to exclude_lines in .coveragerc (described here and here):

[report]
exclude_lines =
    # Skip any pass lines such as may be used for @abstractmethod
    pass

    # 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__.:
14reactions
coulingcommented, Aug 13, 2021

Another approach, which I now prefer, is to remove the pass altogether. It’s not required if the method has a doc string.

Future readers may prefer to simply document their abstract methods rather than leaving them empty. So this can be changed:

class Foo(ABC):
    @abstractmethod
    def bar(self):
        pass

To this:

class Foo(ABC):
    @abstractmethod
    def bar(self):
       """
       Do xyz to this object
       """

Some may prefer to leave pass showing red in the coverage report as a gentle reminder to document.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Excluding abstractproperties from coverage reports
on my project, it complains that the property def line is untested. It can't actually be tested (AFAIK) as instantiation of the abstract...
Read more >
JaCoCo - FAQ
Why are abstract methods not shown in coverage reports? Abstract methods do not contain code, therefore code coverage cannot be evaluated. Indeed code...
Read more >
Writing the title and abstract for a research paper - NCBI - NIH
The “abstract” needs to be simple, specific, clear, unbiased, honest, concise, precise, stand-alone, complete, scholarly, (preferably) structured, and should ...
Read more >
Instructions for Authors | JAMA Dermatology
Case reports are not included in this section; see Observation. 1200 words; 15 references; ≤3 tables and/or figures; Structured abstract; Key Points ...
Read more >
Configuration reference — Coverage.py 6.4.4 documentation
Coverage.py options can be specified in a configuration file. ... Don't complain about abstract methods, they aren't run: @(abc\.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found