我有以下通过py.test运行的UnitSest代码。仅仅存在构造函数,就可以使整个班级在运行时跳过py.test -v -s
收集的0项/1跳过
任何人都可以向我解释一下py.test的这种行为?
我有兴趣理解py。测试行为,我知道不需要构造函数。
谢谢zdenek
class TestClassName(object):
def __init__(self):
pass
def setup_method(self, method):
print "setup_method called"
def teardown_method(self, method):
print "teardown_method called"
def test_a(self):
print "test_a called"
assert 1 == 1
def test_b(self):
print "test_b called"
assert 1 == 1
py.test的文档说py.test实现以下标准测试发现:
- 收集从可能是目录,文件名或测试ID的初始命令行参数开始。重复到目录中,除非它们匹配norecursedirs
- 测试_ *。py或 *_test.py文件,由其软件包名称导入。
-
Test
前缀测试类(没有__init__
方法)[&lt ;--在此处注意] ] -
test_
前缀测试功能或方法是测试项目
因此,不需要构造函数,py.test只是忽略了具有构造函数的类。还有一个用于更改标准测试发现的指南。
正如Matti Lyra py的答案中已经提到的,有意跳过具有构造函数的类。这样做的原因是,类仅出于py.test中的结构原因而使用,并且没有任何固有的行为,而实际编写代码时,没有.__init__()
的方法相反,并且更稀有。因此,实际上,使用构造函数跳过一堂课可能是所需的,通常只是一个恰好具有冲突的班级。
最后是py.test需要实例化类以执行测试。如果构造函数采用任何参数,则无法实例化,因此再次跳过是正确的事情。
上述所有答案清楚地解释了根本原因,我只是想分享我的经验并解决警告。
我通过与导入类的混合来使我的测试工作没有警告
from app.core.utils import model_from_meta
from app.core.models import Panel, TestType as _TestType
from app.core.serializers import PanelSerializer, TestType as _TestTypeSerializer
def test_model_from_meta():
assert (Panel is model_from_meta(PanelSerializer))
assert (_TestType is model_from_meta(_TestTypeSerializer))
使用别名导入类后,警告不再打印
我希望这对某人有帮助。
在我的情况下,我碰巧有一个参数的类名称TestParams
,它与pytest
冲突,寻找以test...
的名称开始的类。
解决方案:重命名自己的类
源