预处理pytest实例化父级测试类

问题描述 投票:0回答:1

测试案例是在父母中定义的。每个孩子都会实施某种不同的设置。

当我使用pytest(
python -m pytest
)进行测试时,我希望在Child1 -Child3中进行继承的测试,但是在父母中定义的测试不应运行。他们也不应产生失败的测试。

我该怎么做? 我在

python_classes=

中使用

pytest.ini
,因此未根据名称选择测试。更改这不是一个选择。

我尝试了:

制作父母摘要。结果:父母无法实例化,测试被标记为失败。

在父母中设置
__test__ = False
。结果:儿童测试也未执行。

如果可以在父类中标记为“通过”的测试,则可以使用以下内容。
    eption1:让抽象方法只有在儿童类中未实现的情况下才会失败
  • class Parent(unittest.TestCase): def my_abstract_method(self): if(type(self) == Parent): return else: raise Exception("Please implement in sub class.")
  • eption2:让测试未在父级中进行
  • class Parent(unittest.TestCase): def test_1(self): if(type(self) == Parent): return ...
python pytest
1个回答
0
投票
trory使用超级阶级以区分父母和子女测试

class TestBase: def run_test(self): return self.__class__ != Parent # True for child classes, False for Parent class Parent(TestBase, unittest.TestCase): def setUp(self): if not self.run_test(): self.skipTest("Parent test") class Child1(Parent): pass

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.