【问题标题】:Pytest not collecting staticmethodPytest没有收集静态方法
【发布时间】:2023-03-13 21:40:01
【问题描述】:

我有一个带有一些静态方法和普通方法的测试类。问题是pytest 没有收集静态方法。我在文档中找不到与此相关的任何内容。我怎样才能让它也收集静态方法?

class TestFoo(object):
    @staticmethod
    def test_bar():
        assert 1 == 1

    def test_bar2(self):
        assert 1 == 1

在上面的类中,只收集了test_bar2test_bar() 没有。

我正在运行Python 2.7.13, pytest-3.1.2, py-1.4.34, pluggy-0.4.0

插件是xdist-1.17.1, leaks-0.2.2, cov-2.5.1

【问题讨论】:

    标签: python static-methods pytest


    【解决方案1】:

    收集测试函数时,pytest ensure each functions are callable

    但是 staticmethod 是不可调用的,来自https://docs.python.org/3/reference/datamodel.html:

    静态方法对象本身不可调用,尽管它们包装的对象通常是可调用的。

    见:

    >>> class TestFoo(object):
    ...     @staticmethod
    ...     def test_bar():
    ...         assert 1 == 1
    ... 
    >>> hasattr(TestFoo.__dict__['test_bar'], '__call__')
    False
    

    为此,应该修改 pytest 本身以接受静态方法,我不知道这是否是他们想要的,如果你认为你真的需要它,你可以在 github 上的问题跟踪器上打开一个问题。

    为什么您认为静态方法是一种解决方案?究竟是针对哪个问题?

    【讨论】:

    • 讨论我们是否应该使用静态方法或者它们是否解决任何问题都不是重点。如果该方法不使用 self.不过感谢您的回答!
    猜你喜欢
    • 2013-04-01
    • 1970-01-01
    • 2016-02-02
    • 2018-09-21
    • 2018-05-20
    • 2016-12-27
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多