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 .
![[download white paper] does your customer relationship management (CRM) really](/img/e3/f130d071afb7309fdbf8a9c65b1d38.jpg)
![[source code attached] Intelligent Recommendation System Based on knowledge map -sylvie rabbit](/img/3e/ab14f3a0ddf31c7176629d891e44b4.png)



![ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience](/img/22/08617736a8b943bc9c254aac60c8cb.jpg)
![[uc/os-iii] chapter 1.2.3.4 understanding RTOS](/img/33/1d94583a834060cc31cab36db09d6e.jpg)

![Acwing game 58 [End]](/img/16/c55e0a7aedc354f1c739637ed13a6b.png)