重复测试的pytest参数执行顺序好像不对 [英] pytest parameters execution order for repeated test seems to be wrong

查看:65
本文介绍了重复测试的pytest参数执行顺序好像不对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重复运行参数化的测试.执行测试时似乎没有遵循顺序.我尝试使用 pytest-repeat 和 pytest.mark.parametrize 但我仍然没有得到想要的结果.代码是

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

当我执行

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" 取消设置数据那样工作,因此我需要维护订购以便我可以运行重复测试.这些测试主要用于性能分析,它测量通过 API 设置数据和清除数据的时间.非常感谢任何帮助.提前致谢

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

推荐答案

我终于找到了解决方案.我没有在 conftest 中使用 scenarios,而是在测试中移动它,然后使用 pytest.mark.parametrize.pytest 对参数进行分组,而不是作为一个列表一一执行.无论如何,我实现的方式如下:记住计数顺序应该接近测试方法

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"

输出将是

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参数执行顺序好像不对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆