【问题标题】:pytest parameters execution order for repeated test seems to be wrong重复测试的pytest参数执行顺序似乎是错误的
【发布时间】:2016-05-12 09:35:27
【问题描述】:

我正在尝试重复运行一个参数化的测试。执行测试时似乎没有遵循顺序。我尝试使用 pytest-repeat 和 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.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 设置数据和清除数据的时间。非常感谢任何帮助。提前致谢

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    我终于找到了一种方法。我没有在 conftest 中使用 scenarios,而是在测试中将其移动,然后使用 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]
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      相关资源
      最近更新 更多