pytest参数重复测试的执行顺序好像不对
pytest parameters execution order for repeated test seems to be wrong
我正在尝试 运行 反复进行参数化测试。执行测试时似乎没有遵循顺序。我尝试使用 pytest-repeat 和 pytest.mark.parametrize 但我仍然没有得到想要的结果。
代码是
conftest.py
scenarios = [('first', {'attribute': 'value'}), ('second', {'attribute': 'value'})]
id = [scenario[0] for scenario in scenarios]
@pytest.fixture(scope="function", params=scenarios, ids=id)
def **get_scenario**(request):
return request.param
test_param.py
@pytest.mark.parametrize("count",range(3))
def test_scenarios(get_scenario,count):
assert get_scenario
当我执行
py.test test_param.py
我得到的结果是
test_scenarios[first-0]
test_scenarios[first-1]
test_scenarios[first-2]
test_scenarios[second-0]
test_scenarios[second-1]
test_scenarios[second-2]
我期待的结果是
test_scenarios[first-0]
test_scenarios[second-0]
test_scenarios[first-1]
test_scenarios[second-1]
test_scenarios[first-2]
test_scenarios[second-2]
有什么方法可以让它像测试 "first" 设置日期和 "second"[=36 一样工作=] 取消设置数据,因此我需要维护顺序以便我可以 运行 重复测试。这些测试基本上用于性能分析,通过API测量设置数据和清除数据的时间。非常感谢任何帮助。提前致谢
我终于找到了一种方法。我没有在 conftest 中使用 scenarios,而是在测试中移动它,然后使用 pytest.mark.parametrize。 pytest 将参数分组,而不是作为一个列表一个一个地执行。无论如何,我实现的方式如下:记住计数顺序应该接近测试方法
test_param.py
scenarios = [('first', {'attribute': 'value'}), ('second', {'attribute': 'value'})]
@pytest.mark.parametrize("test_id,scenario",scenarios)
@pytest.mark.parametrize("count",range(3)) #Remember the order , if you move it first then it will not run it the order i desired i-e first,second,first second
def test_scenarios(test_id,scenario,count):
assert scenario["attribute"] == "value"
输出将是
test_scenario[0-first-scenario0]
test_scenario[0-second-scenario1]
test_scenario[1-first-scenario0]
test_scenario[1-second-scenario1]
test_scenario[2-first-scenario0]
test_scenario[2-second-scenario1]
希望对您有所帮助
我正在尝试 运行 反复进行参数化测试。执行测试时似乎没有遵循顺序。我尝试使用 pytest-repeat 和 pytest.mark.parametrize 但我仍然没有得到想要的结果。 代码是
conftest.py
scenarios = [('first', {'attribute': 'value'}), ('second', {'attribute': 'value'})]
id = [scenario[0] for scenario in scenarios]
@pytest.fixture(scope="function", params=scenarios, ids=id)
def **get_scenario**(request):
return request.param
test_param.py
@pytest.mark.parametrize("count",range(3))
def test_scenarios(get_scenario,count):
assert get_scenario
当我执行
py.test test_param.py
我得到的结果是
test_scenarios[first-0]
test_scenarios[first-1]
test_scenarios[first-2]
test_scenarios[second-0]
test_scenarios[second-1]
test_scenarios[second-2]
我期待的结果是
test_scenarios[first-0]
test_scenarios[second-0]
test_scenarios[first-1]
test_scenarios[second-1]
test_scenarios[first-2]
test_scenarios[second-2]
有什么方法可以让它像测试 "first" 设置日期和 "second"[=36 一样工作=] 取消设置数据,因此我需要维护顺序以便我可以 运行 重复测试。这些测试基本上用于性能分析,通过API测量设置数据和清除数据的时间。非常感谢任何帮助。提前致谢
我终于找到了一种方法。我没有在 conftest 中使用 scenarios,而是在测试中移动它,然后使用 pytest.mark.parametrize。 pytest 将参数分组,而不是作为一个列表一个一个地执行。无论如何,我实现的方式如下:记住计数顺序应该接近测试方法
test_param.py
scenarios = [('first', {'attribute': 'value'}), ('second', {'attribute': 'value'})]
@pytest.mark.parametrize("test_id,scenario",scenarios)
@pytest.mark.parametrize("count",range(3)) #Remember the order , if you move it first then it will not run it the order i desired i-e first,second,first second
def test_scenarios(test_id,scenario,count):
assert scenario["attribute"] == "value"
输出将是
test_scenario[0-first-scenario0]
test_scenario[0-second-scenario1]
test_scenario[1-first-scenario0]
test_scenario[1-second-scenario1]
test_scenario[2-first-scenario0]
test_scenario[2-second-scenario1]
希望对您有所帮助