从覆盖率报告中排除抽象属性

发布于 2025-01-03 10:26:47 字数 363 浏览 1 评论 0原文

我有一个抽象基类,大致如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

(り薆情海 2025-01-10 10:26:47

对我来说,最好的解决方案是 @Wesley 在对已接受答案的评论中提到的,特别是用抽象属性的文档字符串替换“pass”,例如:

class MyAbstractClass(object):
    __metaclass__ = ABCMeta

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

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. """
眸中客 2025-01-10 10:26:47

没有办法精确地排除抽象属性,但如果您稍作更改,就可以。让你的抽象属性引发错误:

@abstractproperty
def myproperty(self): 
    raise NotImplementedError

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

[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

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

真心难拥有 2025-01-10 10:26:47

我的 .coveragerc 中有自定义跳过逻辑:

[report]
exclude_lines =
    pragma: no cover
    @abstract

这样所有抽象方法和抽象属性都被标记为已跳过。

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.

魂归处 2025-01-10 10:26:47

直接来自文档。将以下部分添加到您的 pyproject.toml 中:

[tool.coverage.report]
exclude_also = [
    "raise AssertionError",
    "raise NotImplementedError",
    "@(abc\\.)?abstractmethod",
    ]

或添加到 .coveragerc

[report]
exclude_also =
    raise AssertionError
    raise NotImplementedError
    @(abc\.)?abstractmethod

Straight from the docs. Add to your pyproject.toml the following section:

[tool.coverage.report]
exclude_also = [
    "raise AssertionError",
    "raise NotImplementedError",
    "@(abc\\.)?abstractmethod",
    ]

or to .coveragerc

[report]
exclude_also =
    raise AssertionError
    raise NotImplementedError
    @(abc\.)?abstractmethod
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文