utk*_*sal 3 python static-methods pytest
我有一个测试类,其中包含一些静态方法和普通方法。问题是pytest没有收集静态方法。我在文档中找不到与此相关的任何内容。我怎样才能让它也收集静态方法?
class TestFoo(object):
@staticmethod
def test_bar():
assert 1 == 1
def test_bar2(self):
assert 1 == 1
Run Code Online (Sandbox Code Playgroud)
在上面的类中,只有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
在收集测试函数时,pytest 确保每个函数都是可调用的。
但静态方法不可调用,来自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
Run Code Online (Sandbox Code Playgroud)
为此,应该修改 pytest 本身以接受静态方法,我不知道这是否是他们想要的,如果你认为你确实需要它,你可以在 github 上的问题跟踪器上打开一个问题。
为什么您认为静态方法是一种解决方案?具体针对哪个问题?
| 归档时间: |
|
| 查看次数: |
3103 次 |
| 最近记录: |