从覆盖率报告中排除abstractproperties [英] Excluding abstractproperties from coverage reports

查看:123
本文介绍了从覆盖率报告中排除abstractproperties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的抽象基类:

I have an abstract base class along the lines of:

class MyAbstractClass(object):
    __metaclass__ = ABCMeta

    @abstractproperty
    def myproperty(self): pass

但是,当我在项目上进行鼻子测试(覆盖率)时,它抱怨未定义属性def行。不能实际上进行测试(AFAIK),因为抽象类的实例化将导致引发异常。

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..

有没有解决方法,还是我只需要接受< 100%的测试覆盖率?

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

当然,我可以删除 ABCMeta 的用法,只需将基类提高 NotImpementedError ,但我更喜欢前一种方法。

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

推荐答案

完全按照您的需要排除抽象属性,但是如果进行了一些更改,则可以。让您的抽象属性引发错误:

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

然后,您可以指示coverage.py忽略引发NotImplementedError的行。创建一个.coveragerc文件,并放入其中:

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

有关您可能希望始终忽略的行类型的更多信息,请参见: http://nedbatchelder.com/code/coverage/config.html

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

这篇关于从覆盖率报告中排除abstractproperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆