我试图反复进行一次测试,这是一个参数化的测试。在执行测试时,它似乎没有遵循顺序。我尝试使用pytest-重复和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.paramtest_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]有什么方法可以让它像测试那样工作吗?首先,“设置日期,”“第二”取消数据,因此我需要维护顺序,以便能够运行重复测试。这些测试基本上是用于性能分析,它通过API度量设置数据和清除数据的时间。任何帮助都是非常感谢的。提前感谢
发布于 2016-05-20 10:18:42
最后我找到了一种方法。我在测试中没有使用场景,而是在测试中移动它,然后使用pytest.mark.parametrize。pytest将params分组,而不是一个一个地执行列表。无论如何,我实现的方式如下:记住,计数顺序应该接近测试方法
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]希望这能有所帮助
https://stackoverflow.com/questions/37182929
复制相似问题