如何用多个参数化修饰一个测试以避免特定情况的失败?



是否有办法在pytestxfail满足特定参数化条件时的测试?
注意参数化是在fixture上,而不是在mark.parametrize上。

from pytest import fixture, xfail
@fixture(params=['a', 'b'])
def setup_1(request):
return request.param
@fixture(params=['x', 'y'])
def setup_2(request):
return request.param
@xfail() # Something here ??
def test_something(setup_1, setup_2):
...  # Asserrt something
我想修改上面的例子,所以测试是xfail当,例如setup_1='a'setup_2='x'?根据pytest文档,我知道一个选项是在测试中使用一个条件:
def test_something(setup_1, setup_2):
if setup_1 == 'a' and setup_2 == 'x':
pytest.xfail("failing configuration (but should work)")
else:
...  # Assert something

然而,我想知道是否可能有一个更可读的方式使用装饰符。

我认为这是不可能做到的,你打算做的参数来自多个参数化的fixtures。但是,即使这不是您想要做的,也值得强调的是,您可以按照文档中的说明对xfail进行参数化。

@pytest.mark.parametrize(
("setup_1", "setup_2"), [
pytest.param("a", "x", marks=pytest.mark.xfail(reason="some bug")),
("a", "y"),
("b", "x"),
("b", "y"),
]
)
def test_something(setup_1, setup_2):
print(f"{setup_1}, {setup_2}")

不幸的是,据我所知,它不适用于参数化的fixtures。综上所述,如果您需要来自fixture的参数,那么您的选项(测试方法中的条件)似乎是最佳解决方案。

相关内容

最新更新