我的问题here询问在接收参数化参数的类上使用增量装饰器。我描述了参数化如何导致类为每个参数运行第一个函数,然后只为每个参数运行第二个函数,等等。我收到了answer如何更改函数调用的顺序,以便它去function1(param1),function2(param1),function1(param2),function2(param2)。
不幸的是,这并没有解决潜在的问题。使用答案中的代码:
# content of conftest.py
import pytest
def pytest_generate_tests(metafunc):
idlist = []
argvalues = []
for scenario in metafunc.cls.scenarios:
idlist.append(scenario[0])
items = scenario[1].items() #pretty sure this is NOT the same "item" as in function below
argnames = [x[0] for x in items]
argvalues.append(([x[1] for x in items]))
metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")
def pytest_runtest_makereport(item, call):
if "incremental" in item.keywords:
if call.excinfo is not None:
parent = item.parent
parent._previousfailed = item
def pytest_runtest_setup(item):
if "incremental" in item.keywords:
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail("previous test failed (%s)" %previousfailed.name)
# content of test_scenarios.py
import pytest
scenario1 = ('basic', {'attribute': 'value'})
scenario2 = ('advanced', {'attribute': 'value2'})
@pytest.mark.incremental
class TestSampleWithScenarios(object):
scenarios = [scenario1, scenario2]
def test_demo1(self, attribute):
assert attribute=="value2"
def test_demo2(self, attribute):
assert isinstance(attribute, str)
我得到的是:
test_demo1-失败
test_demo2- xfailed
test_demo1- xfailed(应该通过)
test_demo2- xfailed(应该通过)
这是因为pytest正在检查父级先前是否已经失败,并且这不会依赖于新一轮的参数。
有没有办法重置先前失败的标记,以便增量提供所需的报告?