从覆盖率报告中排除抽象属性
我有一个抽象基类,大致如下:
class MyAbstractClass(object):
__metaclass__ = ABCMeta
@abstractproperty
def myproperty(self): pass
但是当我在我的项目上运行nosetests(覆盖范围)时,它抱怨属性定义行未经测试。它实际上无法被测试(AFAIK),因为抽象类的实例化将导致引发异常。
是否有任何解决方法,或者我只需要接受 100% 测试覆盖率?
当然,我可以删除 ABCMeta 用法并简单地让基类引发 NotImpementedError ,但我更喜欢前一种方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说,最好的解决方案是 @Wesley 在对已接受答案的评论中提到的,特别是用抽象属性的文档字符串替换“pass”,例如:
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.:
没有办法精确地排除抽象属性,但如果您稍作更改,就可以。让你的抽象属性引发错误:
然后你可以指示coverage.py忽略引发NotImplementedError的行。创建一个 .coveragerc 文件,并在其中放入:
有关您可能希望始终忽略的行类型的更多想法,请参阅: http://nedbatchelder.com/code/coverage/config.html
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:
Then you can instruct coverage.py to ignore lines that raise NotImplementedError. Create a .coveragerc file, and in it put:
For more ideas about the kinds of lines you might want to always ignore, see: http://nedbatchelder.com/code/coverage/config.html
我的
.coveragerc
中有自定义跳过逻辑:这样所有抽象方法和抽象属性都被标记为已跳过。
I have custom skip logic in my
.coveragerc
:This way all abstractmethods and abstractproperties are marked as skipped.
直接来自文档。将以下部分添加到您的
pyproject.toml
中:或添加到
.coveragerc
Straight from the docs. Add to your
pyproject.toml
the following section:or to
.coveragerc