zengxuejie

导航

pytest(6):ordering控制用例的执行顺序

前言

  pytest框架在运行测试用例时,有时并不是完全按照顺序进行执行的, 是乱序的,因此有时我们想指定用例的顺序时可以使用pytest-order插件。

安装方式:  

pip install pytest-ordering

使用方法:

  在指定的用例方法前加上装饰器 @pytest.mark.run(order=[num])  ,设置num值,num越小越先执行。

 1 # Author xuejie zeng
 2 # encoding utf-8
 3 
 4 import pytest
 5 
 6 @pytest.mark.run(order=2)
 7 def test_a():
 8     print("我是用例a")
 9     assert True
10 
11 @pytest.mark.run(order=1)
12 def test_b():
13     print("我是用例b")
14     assert True

运行结果:

1 collected 2 items                                                                                                                                         
2 
3 test-or.py::test_b 我是用例b
4 PASSED
5 test-or.py::test_a 我是用例a
6 PASSED

也可以加负值,负值越小越先执行

 1 # Author xuejie zeng
 2 # encoding utf-8
 3 
 4 import pytest
 5 
 6 @pytest.mark.run(order=-2)
 7 def test_a():
 8     print("我是用例a")
 9     assert True
10 
11 @pytest.mark.run(order=-1)
12 def test_b():
13     print("我是用例b")
14     assert True
15 
16 @pytest.mark.run(order=-4)
17 def test_c():
18     print("我是用例c")
19     assert True

运行结果:

1 collected 3 items                                                                                                                                         
2 
3 test-or.py::test_c 我是用例c
4 PASSED
5 test-or.py::test_a 我是用例a
6 PASSED
7 test-or.py::test_b 我是用例b
8 PASSED

 

关注个人公众号:测试开发进阶之路

  

posted on 2020-09-17 14:28  曾小懒  阅读(449)  评论(0)    收藏  举报