当前位置:网站首页>Pytest (4) - test case execution sequence

Pytest (4) - test case execution sequence

2022-07-05 02:43:00 Here is a blank sheet of paper for you

Preface

In the last article, we talked about pytest Naming rules of test cases in , So in pytest In what order are the test cases executed in ?

stay unittest In the frame , The default in accordance with the ACSII Load test cases in the order of code and execute , In sequence :09、AZ、a~z, Test directory 、 Test module 、 Test class 、 The test method / Test functions load test cases according to this rule .

and pytest The execution sequence of use cases in is the same as unittest It's different ,pytest There is a default execution order , You can also customize the execution order .

pytest Default execution order

  • Test directory 、 Test module , Execute in sort order

    The order of execution is as follows :

  • Execution sequence under the same test module

    import pytest
    
    class TestOrder:
    
        def test_e(self):
            print("test_e")
    
        def test_4(self):
            print("test_4")
    
    
    def test_b():
        print("test_a")
    
    def test_a():
        print("test_a")
    
    def test_2():
        print("test_2")
    
    def test_1():
        print("test_1")
    
    
    if __name__ == '__main__':
        pytest.main()
    

    The order of execution is as follows :

Custom execution order

pytest The framework supports the execution sequence of custom test cases , Need to install pytest-ordering plug-in unit .

install

pip install pytest-ordering

Use

Need to use @pytest.mark.run(), The code is as follows :

import pytest

class TestOrder:

    def test_e(self):
        print("test_e")

    def test_4(self):
        print("test_4")


def test_b():
    print("test_a")

@pytest.mark.run(order=2)
def test_a():
    print("test_a")

@pytest.mark.run(order=1)
def test_2():
    print("test_2")

def test_1():
    print("test_1")


if __name__ == '__main__':
    pytest.main()

The order of execution is as follows :

In the test module , First of all, to be @pytest.mark.run() Marking test methods / Test functions , Then perform other tasks in the default order .

summary

although pytest You can customize the execution order of test cases , But in the process of actual test case design , Use cases should not be executed in sequence , That is, any individual test case is an independent and complete verification of function points , Not dependent on other use cases .

原网站

版权声明
本文为[Here is a blank sheet of paper for you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140901048482.html