亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python自動化測試pytest中fixtureAPI簡單說明

 更新時間:2021年10月09日 11:22:02   作者:愛測試的高胖胖  
這篇文章主要為大家介紹了Python自動化測試pytest中fixtureAPI的簡單說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步

什么是fixture

根據(jù)pytest官方文檔的說明,fixture可以簡單的歸納為具有以下功能的函數(shù):

  • 配置測試前系統(tǒng)的初始狀態(tài);
  • 定義傳入測試中的數(shù)據(jù)集;
  • 為批量測試提供數(shù)據(jù)源等

與xUnit風(fēng)格的setup和teardown的對比

fixture的功能與setup和teardown類似,可以實現(xiàn)setup和teardown的功能,但是對這些功能進行了明顯的改進,主要有以下方面:

  • 調(diào)用靈活??梢栽跍y試函數(shù)、模塊、類或整個項目中聲明fixture的名稱來進行調(diào)用;
  • 使用靈活。fixture即適用于簡單的單元測試又適用于復(fù)雜的功能測試。根據(jù)測試的需求可對fixture進行參數(shù)化使用,并且可對fixture進行重復(fù)使用。
  • fixture是以模塊化方式實現(xiàn)的,因此允許fixture調(diào)用其他fixture函數(shù);
  • teardown的實現(xiàn)邏輯更加清晰明了,并且方便進行管理。

 fixture運行報錯后,pytest的處理方式

通過上面的說明,我們可以知道fixture函數(shù)本身是允許調(diào)用其他fixture函數(shù)的。在這種情況下,測試運行的時候,其中一個fixture函數(shù)報錯了,pytest的會如何處理呢?
通過pytest官方文檔的說明,我們可以知道:

  • pytest以線性的方式順序執(zhí)行測試用例所調(diào)用的fixture函數(shù);
  • 當(dāng)順序較前的fixture函數(shù)執(zhí)行報錯后,pytest會停止執(zhí)行該測試所調(diào)用的其他fixture,并且將測試標記出錯;
  • 測試標記錯誤,并不意味著測試未通過,只能說明測試無法嘗試執(zhí)行下去,因此我們需要盡可能的去為測試函數(shù)減少必要的依賴關(guān)系。

示例1:

1.在以下demo代碼中,order()返回類型存在問題,正確的應(yīng)該返回一個list,我們給其返回一個None:

import pytest
@pytest.fixture
def order():
    return None  #正確應(yīng)該返回[],我們給返回一個None
@pytest.fixture
def append_first(order):
    order.append(1)
@pytest.fixture
def append_second(order, append_first):
    order.extend([2])
@pytest.fixture(autouse=True)
def append_third(order, append_second):
    order += [3]
def test_order(order):
    assert order == [1, 2,3]

運行后結(jié)果如下:

test_order被標記Error,并且信息提示:test setup failed,說明是調(diào)用的fixture函數(shù)存在問題,且說明了錯誤原因。

在這里插入圖片描述

2.如果是test_order運行未通,運行信息會怎么樣提醒呢?我們按照以下demo修改測試代碼,修改test_order的斷言語句:

import pytest
@pytest.fixture
def order():
    return []   #返回一個list
@pytest.fixture
def append_first(order):
    order.append(1)
@pytest.fixture
def append_second(order, append_first):
    order.extend([2])
@pytest.fixture(autouse=True)
def append_third(order, append_second):
    order += [3]
def test_order(order):
    assert order == [1, 2]  #斷言失敗,正確應(yīng)該是 order==[1,2,3]

運行結(jié)果如下:

test_order被標記failed,且提醒是AssertionError,斷言出錯。這說明是test_order 本身運行未通過。

在這里插入圖片描述

2.fixture API @pytest.fixture()說明

pytest使用@pytest.fixture()來聲明fixture方法。具體如何使用,我會在文章后面進行詳細說明。在此,主要來簡單說明一下fixture()。

def fixture(
    fixture_function: Optional[_FixtureFunction] = None,
    *,
    scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
    params: Optional[Iterable[object]] = None,
    autouse: bool = False,
    ids: Optional[
        Union[
            Iterable[Union[None, str, float, int, bool]],
            Callable[[Any], Optional[object]],
        ]
    ] = None,
    name: Optional[str] = None,
) -> Union[FixtureFunctionMarker, _FixtureFunction]:

參數(shù)說明:

2.1 scope

fixture函數(shù)的作用域。作用域從小到大依次為:function(默認)、class、module、packagesession。

還可傳入一個可調(diào)用對象,以實現(xiàn)動態(tài)修改fixture的作用域。

后面會單獨寫一篇文章,為大家詳細介紹fixture的scope。

2.2 params

傳入測試數(shù)據(jù)集,動態(tài)生成測試用例,每一條數(shù)據(jù)都單獨生成一條測試用例。通過request.param,可以獲取傳入的這些數(shù)據(jù)。

后面會單獨寫一篇文章,為大家詳細介紹fixture的參數(shù)化。

2.3 autouse

fixture自動應(yīng)用標識。

如果是True,則在同作用域下的測試函數(shù),會自動調(diào)用該fixture;如果是False,則測試函數(shù)需要主動去調(diào)用該fixture。

后面會在介紹fixture調(diào)用方法的文章給大家詳細說明。

2.4 ids

測試用例ID標識,與parmas傳入的參數(shù)一一對應(yīng)。當(dāng)未定義時,會自動生成id。

示例2:

1.傳入ids參數(shù),運行以下demo:

import pytest
@pytest.fixture(params=[1,2,3],ids=['A','B','C'])
def ids(request):
    data=request.param
    print(f'獲取測試數(shù)據(jù){data}')
    return data
def test_ids(ids):
    print(ids)

運行結(jié)果:

在執(zhí)行信息中,我們可以發(fā)現(xiàn)ids的三個參數(shù)和params的三個參數(shù)一一對應(yīng)顯示,并且ids的參數(shù)作為測試用例id的一部分呈現(xiàn)出來。

在這里插入圖片描述

2. 修改上面demo中的代碼,不傳入ids參數(shù),運行一下:

import pytest
@pytest.fixture(params=[1,2,3]) #未傳入ids
def ids(request):
    data=request.param
    print(f'獲取測試數(shù)據(jù){data}')
    return data
def test_ids(ids):
    print(ids)

運行結(jié)果:

查看運行結(jié)果我們可以發(fā)現(xiàn),雖然沒有傳入ids,但是卻自動生成了ids。

在這里插入圖片描述

測試結(jié)束后,我們常常以測試報告的形式來匯報測試結(jié)果,如結(jié)合allure呈現(xiàn)測試結(jié)果。通過ids傳入的參數(shù)可以對測試用例進行說明,這樣更方便我們查看測試結(jié)果。

2.5 name

fixture的別名。fixture的name默認是@pytest.fixture所裝飾的函數(shù)的函數(shù)名。使用fixture的別名可以提高代碼的閱讀性。

示例3:
以下面的demo為例:

import pytest
@pytest.fixture()
def login():
    print('login')
class SubClass:
    def sub_login(self):
        print('subcalss_login')
class TestCase:
    def test_case1(self,login):  #調(diào)用fixture——login
        login=SubClass()  #定義一個login并實例化SubClass
        login.sub_login() #調(diào)用SubClass中的sub_login()
        print('這是testcase1')

我們定義了一個fixture函數(shù)——login(),同時在test_case1中實例化了一個Subclass類,并起名為login,然后調(diào)用了SubClass類中的sub_login()。如果代碼復(fù)雜的情況,很容易將fixture函數(shù)的login與SubClass實例的login弄混淆,增加代碼的閱讀的復(fù)雜度。

當(dāng)我們使用fixture別名的話,在閱讀代碼的時候就很容易進行區(qū)分。

@pytest.fixture(name='module_login') 
def login():
    print('login')
class TestCase:
    def test_case1(self,module_login):  #使用fixture別名:module_login
        login=SubClass()  #定義一個login并實例化SubClass
        login.sub_login() #調(diào)用SubClass中的sub_login()
        print('這是testcase1')

注意:

當(dāng)使用name參數(shù)后,則無法再通過@pytest.fixture所裝飾的函數(shù)的函數(shù)名來進行調(diào)用,必須使用name所指定fixture別名來調(diào)用。

2.6 fixture_function

目前pytest官方文檔未給出具體說明。

文末說明:
以上內(nèi)容是我在閱讀pytest官方文檔后,依照個人理解進行整理。內(nèi)容可能會有理解錯誤之處,歡迎大家留言指正。謝謝!

更多關(guān)于自動化測試pytest中fixtureAPI的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論