首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重复测试的pytest参数执行顺序似乎是错误的。

重复测试的pytest参数执行顺序似乎是错误的。
EN

Stack Overflow用户
提问于 2016-05-12 09:35:27
回答 1查看 971关注 0票数 0

我试图反复进行一次测试,这是一个参数化的测试。在执行测试时,它似乎没有遵循顺序。我尝试使用pytest-重复和pytest.mark.parametrize,但我仍然没有得到想要的结果。代码是

conftest.py

代码语言:javascript
复制
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

代码语言:javascript
复制
@pytest.mark.parametrize("count",range(3))
def test_scenarios(get_scenario,count):
    assert get_scenario

当我执行

代码语言:javascript
复制
py.test test_param.py

我得到的结果是

代码语言:javascript
复制
test_scenarios[first-0]
test_scenarios[first-1]
test_scenarios[first-2]
test_scenarios[second-0]
test_scenarios[second-1]
test_scenarios[second-2]

期望的结果是

代码语言:javascript
复制
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度量设置数据和清除数据的时间。任何帮助都是非常感谢的。提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-20 10:18:42

最后我找到了一种方法。我在测试中没有使用场景,而是在测试中移动它,然后使用pytest.mark.parametrize。pytest将params分组,而不是一个一个地执行列表。无论如何,我实现的方式如下:记住,计数顺序应该接近测试方法

test_param.py

代码语言:javascript
复制
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"

输出将是

代码语言:javascript
复制
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]

希望这能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37182929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档