pytest parameters execution order for repeated test seems to be wrong

烈酒焚心 提交于 2019-12-12 05:59:08

问题


I am trying to run a test repeatedly which is a paramertrized. it doesn't seem to follow the order while executing test. I tried using pytest-repeat and pytest.mark.parametrize but i still didn't get the desired outcome. The code is

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

When i execute

py.test test_param.py

The result i get is

test_scenarios[first-0]
test_scenarios[first-1]
test_scenarios[first-2]
test_scenarios[second-0]
test_scenarios[second-1]
test_scenarios[second-2]

The result iam expecting is

test_scenarios[first-0]
test_scenarios[second-0]
test_scenarios[first-1]
test_scenarios[second-1]
test_scenarios[first-2]
test_scenarios[second-2]

Is there any way i can make it work like as the test "first" sets the date and "second" unsets the data , hence i need to maintain the order so that i can run repeat the tests. These test are basically for performance profiling which measures the time for setting the data and clearing the data by API. Any help is much appreciate. Thanks in advance


回答1:


Finally i found a way to do it. Instead of having scenarios in conftest i moved it in a test and then using pytest.mark.parametrize. pytest groups the params instead of executing as a list one by one. anyway the way i achieved is as follows : Remember the count order should be close to the test method

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"

The output will be

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]

Hope this helps



来源:https://stackoverflow.com/questions/37182929/pytest-parameters-execution-order-for-repeated-test-seems-to-be-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!