Pytest不收集静态方法

时间:2017-06-22 13:14:53

标签: python static-methods pytest

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

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

    def test_bar2(self):
        assert 1 == 1

在上述课程中,仅收集了test_bar2,而test_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

1 个答案:

答案 0 :(得分:2)

收集测试功能时,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上的问题跟踪器上打开一个问题真的需要它。

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