2016-05-12 47 views
0

我试图重复运行一个参数化的测试。它在执行测试时似乎不遵循顺序。我尝试使用pytest-repeat和pytest.mark.parametrize,但我仍然没有得到期望的结果。 代码是pytest参数重复测试的执行顺序似乎是错误的

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] 

结果IAM 期待是

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

有没有什么办法可以使它像工作作为测试“第一”设置日期和“第二”会取消数据,因此我需要维护订单,以便我可以运行重复测试。这些测试基本上是用于性能分析的,它测量了通过API设置数据和清除数据的时间。任何帮助非常感谢。在此先感谢

回答

0

最后,我找到了一种方法来做到这一点。而不是方案 conftest我把它移动到一个测试,然后使用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] 

希望这会有所帮助