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

时间:2018-10-15 07:37:08

标签: python pytest

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

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__构造函数。我想要一种避免这种情况的方法。

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

1 个答案:

答案 0 :(得分:0)

正确使用pytest重用测试方法是使用Fixtures。这是related questionexample solution

感谢@hoefling在评论中指出__init__对测试不利。

相关问题