Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading branch coverage of empty methods #129

Closed
nedbat opened this issue May 18, 2011 · 5 comments
Closed

Misleading branch coverage of empty methods #129

nedbat opened this issue May 18, 2011 · 5 comments
Labels
branch bug Something isn't working

Comments

@nedbat
Copy link
Owner

nedbat commented May 18, 2011

Originally reported by Christian Heimes (Bitbucket: tiran, GitHub: tiran)


Branch coverage of an abc.abstractmethod with just a doc string in the method, shows the decorator line as uncovered branch that never reaches "exit".

#!python
import abc

class AExample(object):
    @abc.abstractmethod  # exit
    def method(self):
        """doc
        """

class Example(AExample):
    def method(self):
        return True

Example().method()

When I replace the doc string with "pass", the decorator line is a covered branch. However the pass statement is not covered at all.

#!python

class AExample(object):
    @abc.abstractmethod 
    def method(self): 
        pass  # uncovered

@nedbat
Copy link
Owner Author

nedbat commented Oct 22, 2013

Turns out this isn't about abstract methods, it's about empty uncalled methods with only a docstring:

class Example(object):
    def method1(self):      # no branch to exit
        """doc"""

    def method2(self):
        pass                # not executed

    def method3(self):
        """doc"""
        pass                # not executed

@nedbat
Copy link
Owner Author

nedbat commented Oct 22, 2013

Original comment by Alex Gaynor (Bitbucket: alex_gaynor, GitHub: Unknown)


It's unclear to me what the correct behavior is, it seems to me it's either a) no executed, or b) covered, but not a branch coverage thing. (b) is somewhat more convenient, but I'm not sure it's more correct.

@nedbat
Copy link
Owner Author

nedbat commented Jul 9, 2014

Original comment by Mike Nerone (Bitbucket: Manganeez, GitHub: Unknown)


(a) would seem more appropriate for a bare function definition with only a docstring, while (b) seems right for an abstractmethod, so perhaps that should be special-cased as the OP suggested.

Incidentally, it's easy enough to do per-project because the "blamed" lines are the ones with the decorators. In your project .coveragerc:

#!
[report]
exclude_lines =
    @abstract
    <whatever_else>

@nedbat
Copy link
Owner Author

nedbat commented Jan 3, 2016

This will be fixed when the ast-branch code is merged.

@nedbat
Copy link
Owner Author

nedbat commented Jan 8, 2016

This is fixed in 44719bd (bb), which will be 4.1

@nedbat nedbat closed this as completed Jan 8, 2016
@nedbat nedbat added major bug Something isn't working branch labels Jun 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant