Excluding abstractproperties from coverage reports

可紊 提交于 2019-12-30 01:39:15

问题


I have an abstract base class along the lines of:

class MyAbstractClass(object):
    __metaclass__ = ABCMeta

    @abstractproperty
    def myproperty(self): pass

But when I run nosetests (which coverage) on my project, it complains that the property def line is untested. It can't actually be tested (AFAIK) as instantiation of the abstract class will result in an exception being raised..

Are there any workarounds to this, or do I just have to accept < 100% test coverage?

Of course, I could remove the ABCMeta usage and simply have the base class raise NotImpementedError, but I prefer the former method.


回答1:


There's no way to exclude the abstract properties precisely as you have it, but if you make a slight change, you can. Have your abstract property raise an error:

@abstractproperty
def myproperty(self): 
    raise NotImplementedError

Then you can instruct coverage.py to ignore lines that raise NotImplementedError. Create a .coveragerc file, and in it put:

[report]
exclude_lines =
    # Have to re-enable the standard pragma
    pragma: no cover

    # Don't complain if tests don't hit defensive assertion code:
    raise NotImplementedError

For more ideas about the kinds of lines you might want to always ignore, see: http://nedbatchelder.com/code/coverage/config.html




回答2:


For me the best solution was what @Wesley mentioned in his comment to the accepted answer, specifically replacing 'pass' with a docstring for the abstract property, e.g.:

class MyAbstractClass(object):
    __metaclass__ = ABCMeta

    @abstractproperty
    def myproperty(self):
       """ this property is too abstract to understand. """



回答3:


I have custom skip logic in my .coveragerc:

[report]
exclude_lines =
    pragma: no cover
    @abstract

This way all abstractmethods and abstractproperties are marked as skipped.



来源:https://stackoverflow.com/questions/9202723/excluding-abstractproperties-from-coverage-reports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!