Pytest:在超类中运行所有测试

2024-09-27 00:22:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想运行从另一个类继承的所有测试方法。有办法吗?例如,对于下面的代码,我希望pytest测试从Test继承的TestTwo和{}的所有测试方法。在

class Test:

    def __init__(self, n):
        self.n = n

    def test_even(self):
        assert self.n % 2 == 0

    def test_not_big(self):
        assert self.n < 100


class TestTwo(Test):

    def __init__(self):
        super(TestTwo, self).__init__(2)


class TestTwelve(Test):

    def __init__(self):
        super(TestTwelve, self).__init__(12)

但是,pytest抱怨存在一个__init__构造函数。我想要个办法来避开这个。在

总体目标是使我的测试更加模块化,这样我就可以在多个类上测试类似的问题。在


Tags: 代码testselfinitpytestdefnotassert

热门问题