在测试类中参数化测试时保持测试执行的顺序 [英] maintaining order of test execution when parametrizing tests in test class
本文介绍了在测试类中参数化测试时保持测试执行的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试参数化我的测试,如下所示
I am trying to parametrize my tests like below
@pytest.mark.parametrize("a,b", test_data)
class TestClass():
def test_A(self,a,b):
# Some Code ..
pass
def test_B(self,a,b):
# Some Code ..
pass
def test_C(self,a,b):
# Some Code ..
pass
我希望我的测试像测试步骤一样按顺序执行,例如
I want my test to be executed in Sequential order like test steps, e.g
test_A
test_B
test_C
test_A
test_B
test_C
....
它们被执行的顺序是
test_A
test_A
...
test_B
test_B
...
test_C
test_C
我尝试过的另一个选项是将我的测试放入如下的 for 循环中
The other option I have tried is by putting my tests in for loop like below
for data in test_data:
a,b = data
def test_A(a,b):
# Some Code ..
pass
def test_B(a,b):
# Some Code ..
pass
def test_C(a,b):
# Some Code ..
pass
这给了我想要的顺序,但测试名称在所有迭代中保持不变,因此它在报告中产生问题.
This give me the desired order but test names remains same in all the iteration so it creates problem in reporting.
推荐答案
我终于能够使用 pytest_generate_tests 钩子实现这一目标.
I was finally able to acheive this using pytest_generate_tests hook.
def pytest_generate_tests(metafunc):
argvalues = []
for data in metafunc.cls.data:
items = data.items()
argnames = [x[0] for x in items]
argvalues.append(([x[1] for x in items]))
metafunc.parametrize(argnames, argvalues, scope="class"
class TestClass:
data = [{'attr_1': 'val_1_1', 'attr_2': 'val_1_2'}, {'attr_1': 'val_2_1', 'attr_2': 'val_2_2'}]
def test_A(self, attr_1, attr_2)
...
def test_B(self, attr_1, attr_2)
...
def test_B(self, attr_1, attr_2)
...
https://pytest.org/latest/example/parametrize.html
这篇关于在测试类中参数化测试时保持测试执行的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文