在pytest中,如果测试类是子类,skipif不能正常工作
通常,我有两个测试类共享除setup_class和teardown_class之外的所有测试用例。这两个测试类对两个需要设置不同的服务器运行相同的客户端操作测试。根据测试运行的地点/时间,我可能想跳过某些测试,因为该服务器可能不可用。所以我想出了一个设计:在pytest中,如果测试类是子类,skipif不能正常工作
class AllTests:
def test_1(self):
...
def test_2(self):
...
@pytest.mark.skipif(condition1)
class TestA(AllTests):
@classmethod
def setup_class(cls):
...
@classmethod
def teardown_class(cls):
...
@pytest.mark.skipif(condition2)
class TestB(AllTests):
@classmethod
def setup_class(cls):
...
@classmethod
def teardown_class(cls):
...
它工作正常,如果没有类被跳过。 但是,如果condition1被满足并且TestA被跳过,那么AllTests中的测试函数也不会运行TestB(这显然不是我想要的!)。
那么如何解决这个问题呢?
或有任何其他设计来满足我的要求(共享所有的测试用例,除了setup_class和teardown_class和他们每个人都应该能够跳过测试类)? “parametrize”是否可用?我试过,但就是不能拿出正确的代码:(
这的确是实现的副作用。类标记只是填充到功能。
使用参数化是一个可行的选择。这是特别好,如果你的安装和拆卸功能是相似的,您可以使用一个固定收益率与参数:
@pytest.fixture(scope="class", params=['a', 'b'])
def do_setup(request):
# setup
a = open(request.param)
yield a # provide the fixture value
# teardown
close(a)
如果你不需要的结果(a
这里),那么你可以使用autouse=True
,并有固定的汽车 - 运行。
欲了解更多信息,请参阅有关灯具的pytest文档:https://docs.pytest.org/en/latest/fixture.html
是。夹具也是我找到的解决方案。谢谢〜 –
也许你可以更多地解释为什么你想这样做,e.h.你跳过条件,数据等 –
@NilsWerner我补充说,在顶部〜 –