pytest 控制用例的执行顺序 –> pytest-order 插件

如不使用插件,则pytest加载的所有用例都是乱序的
如果想控制执行顺序,可使用插件:pytest-order

插件安装

1
pip install pytest-ordering

插件使用方式

需要在用例的方法前加上装饰器:
@pytest.mark.run(order=[number]
通过设置“number”值来控制用例执行顺序

插件使用场景

比如某些页面需要先登录再进行操作,则需要将登陆操作应最先运行

使用案例

创建一个Python文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 首先引入 pytest
import pytest

# 随意创建一个测试类
class Testpytest(object):
   
    # 在需要指定顺序的用例方法前添加装饰器
    @pytest.mark.run(order=1)
    def test_one(self):
        print("第几个执行的用例")
       
    @pytest.mark.run(order=2)
    def test_two(self):
        print("第几个执行的用例")
       
    @pytest.mark.run(order=-1)
    def test_three(self):
        print("第几个执行的用例")

执行顺序:
order = 1 --> order = 2 --> order = -1