婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > pytest fixtures裝飾器的使用和如何控制用例的執行順序

pytest fixtures裝飾器的使用和如何控制用例的執行順序

熱門標簽:滴滴地圖標注公司 如何申請400電話代理 天津塘沽區地圖標注 江門智能電話機器人 甘肅高頻外呼系統 杭州房產地圖標注 智能電話機器人調研 400電話在線如何申請 地圖標注可以遠程操作嗎

pytest fixtures裝飾器

pytest中可以使用@pytest.fixture 裝飾器來裝飾一個方法,被裝飾方法的方法名可以作為一個參數傳入到測試方法中。可以使用這種方式來完成測試之前的初始化,也可以返回數據給測試函數。

將fixture作為函數參數

通常使用setup和teardown來進行資源的初始化,如果有這樣一個場景,測試用例1需要依賴登入功能,測試用例2不需要依賴登入功能,測試用例3需要登入功能,這種場景setup,teardown無法實現,也可以使用pytest fixture功能,在這個方法前面加個@pytest.fixture裝飾器,加了這個裝飾器的方法可以以參數的形式傳到方法里,這個方法就會先執行這個登入方法,再去執行自身的用例步驟,如果沒有傳入這個登入方法就不執行登入操作,直接執行已有的步驟

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


@pytest.fixture()
def login():
 print("這時一個登入的方法")
 return ('tome', '123')


@pytest.fixture()
def operate():
 print("這是登入后的操作")


def test_case1(login, operate):
 print(login)
 print("test_case1,需要登入")


def test_case2():
 print("test_case2,不需要登入")


def test_case3(login):
 print(login)
 print("test_case3,需要登入")

在上面的代碼中,測試用例test_case1 和test_case3 分別增加了login 方法名作為參數,pytest會發現并調用@pytest.fixture標記的login功能,運行測試結果如下:

Testing started at 10:17 ...
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_fixture.py
Launching pytest with arguments C:/Users/wanwen/PycharmProjects/vigo/xuexi/20210123/test_fixture.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_fixture.py 這時一個登入的方法
這是登入后的操作
.('tome', '123')
test_case1,需要登入
.test_case2,不需要登入
這時一個登入的方法
.('tome', '123')
test_case3,需要登入
              [100%]

============================== 3 passed in 0.04s ==============================

Process finished with exit code 0

從上面結果可以看出,test_case1 和test_case3 運行之前執行了login方法,test_case2沒有執行這個方法。

控制用例的執行順序

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

以上就是pytest fixtures裝飾器的使用和如何控制用例的執行順序的詳細內容,更多關于pytest fixtures裝飾器和控制用例的執行順序的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • Python 測試框架unittest和pytest的優劣
  • python pytest進階之conftest.py詳解
  • python pytest進階之fixture詳解
  • python中pytest收集用例規則與運行指定用例詳解
  • python的pytest框架之命令行參數詳解(上)
  • Python pytest裝飾器總結(實例詳解)

標簽:德宏 廊坊 長春 臨汾 重慶 河池 東莞 漢中

巨人網絡通訊聲明:本文標題《pytest fixtures裝飾器的使用和如何控制用例的執行順序》,本文關鍵詞  pytest,fixtures,裝飾,器,的,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《pytest fixtures裝飾器的使用和如何控制用例的執行順序》相關的同類信息!
  • 本頁收集關于pytest fixtures裝飾器的使用和如何控制用例的執行順序的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 来凤县| 临洮县| 怀安县| 朝阳县| 鸡西市| 罗城| 深泽县| 巴林左旗| 兴城市| 新蔡县| 安岳县| 新昌县| 泰宁县| 昌邑市| 封开县| 达州市| 吴江市| 黄石市| 京山县| 勐海县| 特克斯县| 忻州市| 商水县| 七台河市| 象州县| 绍兴县| 宜君县| 惠州市| 朝阳市| 金坛市| 松桃| 渭南市| 曲周县| 阳山县| 山丹县| 辉县市| 云安县| 北海市| 团风县| 伊通| 德昌县|