如果定义了构造函数,py.test 将跳过测试类 [英] py.test skips test class if constructor is defined

查看:75
本文介绍了如果定义了构造函数,py.test 将跳过测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

收集了 0 个项目/1 个跳过

collected 0 items / 1 skipped

谁能向我解释一下 py.test 的这种行为?

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.

谢谢,兹德内克

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 实现了以下标准测试发现:

The documentation for py.test says that py.test implements the following standard test discovery:

  • collection 从初始命令行参数开始,这些参数可能是目录、文件名或测试 ID.递归到目录中,除非它们匹配 norecursedirs
  • test_*.py 或 *_test.py 文件,按包名导入.
  • Test 前缀测试类(没有 __init__ 方法)[<-- 注意这里]
  • 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.
  • Test prefixed test classes (without an __init__ method) [<-- notice this one here]
  • test_ prefixed test functions or methods are test items

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

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

这篇关于如果定义了构造函数,py.test 将跳过测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆