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, 測試用例

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM