溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何正確的使用pytest fixtures裝飾器

發(fā)布時間:2021-01-29 15:51:21 來源:億速云 閱讀:175 作者:Leah 欄目:開發(fā)技術

如何正確的使用pytest fixtures裝飾器?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

pytest fixtures裝飾器

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

將fixture作為函數(shù)參數(shù)

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

#!/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 方法名作為參數(shù),pytest會發(fā)現(xiàn)并調用@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 運行之前執(zhí)行了login方法,test_case2沒有執(zhí)行這個方法。

控制用例的執(zhí)行順序

一、pytest加載所有的用例都是亂序的,如果想指定用例的順序,可以使用pytest-ordering插件,指定用例的執(zhí)行順序只需要在測試用例的方法前面加上裝飾器@pytest.mark.run(order=[num])設置order的對應的num值,它就可以按照num的大小順序來執(zhí)行。

應用場景:有時運行測試用例要指定它的順序,比如有些場景要先需要登入,才能執(zhí)行后面的流程比如購物流程,下單流程,這時就需要指定用例的執(zhí)行順序。通過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裝飾器問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI