pytest学习笔记(控制用例的执行顺序)

一、pytest加载所有的用例都是乱序的,如果想指定用例的顺序,可以使用pytest-ordering插件,指定用例的执行顺序只需要在测试用例的方法前面加上装饰器@pytest.mark.run(order=[num])设置order的对应的num值,它就可以按照num的大小顺序来执行。

应用场景:有时运行测试用例要指定它的顺序,比如有些场景要先需要登入,才能执行后面的流程比如购物流程,下单流程,这时就需要指定用例的执行顺序。通过pytest-ordering这个插件可以完成用例顺序的指定。

 

二、安装

pip install pytest-ordering

 

三、实例

#!/usr/bin/env python
# _*_coding: utf-8 _*_
import pytest


class Testpytest(object):

    @pytest.mark.run(order=-1)
    def test_two(self):
        print("test_two, 测试用例")

    @pytest.mark.run(order=3)
    def test_one(self):
        print("test_one, 测试用例")

    @pytest.mark.run(order=1)
    def test_three(self):
        print("test_three, 测试用例")

四运行结果

Testing started at 15:51 ...
C:\Python\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_order.py
Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_order.py in C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
============================= test session starts =============================
platform win32 -- Python 3.8.0, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: C:\Users\wanwen\PycharmProjects\vigo\xuexi\20210123
plugins: html-2.1.1, metadata-1.11.0, ordering-0.6collected 3 items

test_order.py                                                         [100%]

============================== 3 passed in 0.06s ==============================

Process finished with exit code 0
.test_three, 测试用例
.test_one, 测试用例
.test_two, 测试用例

 

posted @ 2021-01-24 15:49  剑尊  阅读(926)  评论(0编辑  收藏  举报