Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

python - 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
....

The order in which they are getting executed is

test_A
test_A
...
test_B
test_B
...
test_C
test_C

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...