简体   繁体   English

如果定义了构造函数,py.test 将跳过测试 class

[英]py.test skips test class if constructor is defined

I have following unittest code running via py.test.我有以下通过 py.test 运行的单元测试代码。 Mere presence of the constructor make the entire class skip when running py.test -v -s在运行 py.test -v -s 时,仅存在构造函数就会跳过整个 class

collected 0 items / 1 skipped已收集 0 件 / 1 已跳过

Can anyone please explain to me this behaviour of py.test?谁能向我解释一下py.test的这种行为?

I am interested in understanding py.test behaviour, I know the constructor is not needed.我有兴趣了解 py.test 行为,我知道不需要构造函数。

Thanks, 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

The documentation for py.test says that py.test implements the following standard test discovery: py.test 的文档py.test 实现了以下标准测试发现:

  • collection starts from the initial command line arguments which may be directories, filenames or test ids.集合从初始命令行参数开始,这些参数可能是目录、文件名或测试 ID。 recurse into directories, unless they match norecursedirs递归到目录中,除非它们匹配 norecursedirs
  • test_*.py or *_test.py files, imported by their package name. test_*.py 或 *_test.py 文件,按包名导入。
  • Test prefixed test classes (without an __init__ method) [ <-- notice this one here ] Test前缀的测试类(没有__init__方法)[ <-- 注意这里的这个]
  • test_ prefixed test functions or methods are test items test_前缀的测试函数或方法是测试项

So it's not that the constructor isn't needed, py.test just ignores classes that have a constructor.所以并不是不需要构造函数,py.test 只是忽略具有构造函数的类。 There is also a guide for changing the standard test discovery.还有一个更改标准测试发现的 指南

As already mentioned in the answer by Matti Lyra py.test purposely skips classes which have a constructor.正如 Matti Lyra 的回答中已经提到的 py.test 故意跳过具有构造函数的类。 The reason for this is that classes are only used for structural reasons in py.test and do not have any inherent behaviour, while when actually writing code it is the opposite and much rarer to not have an .__init__() method for a class.这样做的原因是类仅用于 py.test 中的结构原因,并且没有任何固有的行为,而在实际编写代码时,则相反,并且类没有.__init__()方法则更为罕见。 So in practice skipping a class with a constructor will likely be what was desired, usually it is just a class which happens to have a conflicting name.因此,在实践中跳过带有构造函数的类很可能是我们想要的,通常它只是一个碰巧名称冲突的类。

Lastly py.test needs to instantiate the class in order to execute the tests.最后 py.test 需要实例化类以执行测试。 If the constructor takes any arguments it can't instantiate it, so again skipping is the right thing to do.如果构造函数接受任何参数,则它无法实例化它,因此再次跳过是正确的做法。

All the above answers clearly explain the underlying cause, I just thought to share my experience and workaround the warnings.以上所有答案都清楚地解释了根本原因,我只是想分享我的经验和解决警告的方法。

I got my test to work without the warnings by aliasing the imported Class我通过别名导入的 Class 使我的测试在没有警告的情况下工作

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))

After importing the class using aliases the warnings no longer get printed使用别名导入 class 后,不再打印警告

I hope this helps someone.我希望这可以帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM