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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21430900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:49:55  来源:igfitidea点击:

py.test skips test class if constructor is defined

pythonpytest

提问by Zdenek Maxa

I have following unittest code running via py.test. Mere presence of the constructor make the entire class skip when running py.test -v -s

我有以下通过 py.test 运行的单元测试代码。运行 py.test -v -s 时,仅存在构造函数就会跳过整个类

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

回答by Matti Lyra

The documentation for py.test saysthat 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. recurse into directories, unless they match norecursedirs
  • test_*.py or *_test.py files, imported by their package name.
  • Testprefixed test classes (without an __init__method) [<-- notice this one here]
  • test_prefixed test functions or methods are test items
  • 集合从初始命令行参数开始,这些参数可能是目录、文件名或测试 ID。递归到目录中,除非它们匹配 norecursedirs
  • test_*.py 或 *_test.py 文件,按包名导入。
  • Test带前缀的测试类(没有__init__方法)[ <-- 请注意这里]
  • test_带前缀的测试函数或方法是测试项

So it's not that the constructor isn't needed, py.test just ignores classes that havea constructor. There is also a guidefor changing the standard test discovery.

所以并不是不需要构造函数,py.test 只是忽略具有构造函数的类。还有一个更改标准测试发现的指南

回答by flub

As already mentioned in the answer by Matti Lyra py.test purposely skips classes which have a constructor. 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. 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.

正如 Matti Lyra 在回答中已经提到的 py.test 故意跳过具有构造函数的类。这样做的原因是类仅用于 py.test 中的结构原因,并且没有任何固有行为,而在实际编写代码时,则相反,没有.__init__()类的方法则更为罕见。因此,在实践中跳过带有构造函数的类很可能是我们想要的,通常它只是一个碰巧名称冲突的类。

Lastly py.test needs to instantiate the class in order to execute the tests. If the constructor takes any arguments it can't instantiate it, so again skipping is the right thing to do.

最后 py.test 需要实例化类以执行测试。如果构造函数接受任何参数,则它无法实例化它,因此再次跳过是正确的做法。