Python - 使用pytest来测试用子类编写的测试方法 [英] Python - Use pytest to test test methods written in subclass

查看:2215
本文介绍了Python - 使用pytest来测试用子类编写的测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pytest的新手。我有一个场景,我想测试一些子类中编写的测试方法。

I am a novice to pytest. I have a scenario wherein i wanted to test some test methods written in a subclass.

假设以下是我的代码结构

Assume that the following is my code structure

class Superclass:
    def __init__(self, a):
        self.a = a
    def dummy_print():
        print("This is a dummy function")

class TestSubClass(Superclass):

def test_1_eq_1():
    assert 1 == 1

执行以下命令后

py.test -s -v test_filename.py

我得到以下错误messgae :

I get the following error messgae:


无法收集测试类'Test_Super',因为它有一个 init 构造函数

pytest文档中也提到了相同的内容。

The same is mentioned in the pytest documentation as well.

是否有解决方法?

我需要超类' __ init __()方法,因为我的所有测试文件都是你的ld需要从超级类继承。

I need the superclass' __init__() method because all my test files would need to inherit from the super class.

推荐答案

Pytest是不同的野兽而不是单位测试。它禁止类层次结构,您可以在警告消息中看到。

Pytest is different beast than, say unittests. It prohibits class hierarchies, which you can see in the warning message.

如果您需要一些预初始化步骤,请使用灯具

If you need some pre-initialization steps, do them using fixtures:

@pytest.fixture(autouse=True)
def pre_run_initialization():
    # do before test
    yield
    # do after test

这篇关于Python - 使用pytest来测试用子类编写的测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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