欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python自動(dòng)化測(cè)試pytest中fixtureAPI簡(jiǎn)單說(shuō)明

 更新時(shí)間:2021年10月09日 11:22:02   作者:愛(ài)測(cè)試的高胖胖  
這篇文章主要為大家介紹了Python自動(dòng)化測(cè)試pytest中fixtureAPI的簡(jiǎn)單說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步

什么是fixture

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

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

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

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

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

 fixture運(yùn)行報(bào)錯(cuò)后,pytest的處理方式

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

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

示例1:

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

import pytest
@pytest.fixture
def order():
    return None  #正確應(yīng)該返回[],我們給返回一個(gè)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]

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

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

在這里插入圖片描述

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

import pytest
@pytest.fixture
def order():
    return []   #返回一個(gè)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]

運(yùn)行結(jié)果如下:

test_order被標(biāo)記failed,且提醒是AssertionError,斷言出錯(cuò)。這說(shuō)明是test_order 本身運(yùn)行未通過(guò)。

在這里插入圖片描述

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

pytest使用@pytest.fixture()來(lái)聲明fixture方法。具體如何使用,我會(huì)在文章后面進(jìn)行詳細(xì)說(shuō)明。在此,主要來(lái)簡(jiǎn)單說(shuō)明一下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ù)說(shuō)明:

2.1 scope

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

還可傳入一個(gè)可調(diào)用對(duì)象,以實(shí)現(xiàn)動(dòng)態(tài)修改fixture的作用域。

后面會(huì)單獨(dú)寫(xiě)一篇文章,為大家詳細(xì)介紹fixture的scope。

2.2 params

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

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

2.3 autouse

fixture自動(dòng)應(yīng)用標(biāo)識(shí)。

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

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

2.4 ids

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

示例2:

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

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

運(yùn)行結(jié)果:

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

在這里插入圖片描述

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

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

運(yùn)行結(jié)果:

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

在這里插入圖片描述

測(cè)試結(jié)束后,我們常常以測(cè)試報(bào)告的形式來(lái)匯報(bào)測(cè)試結(jié)果,如結(jié)合allure呈現(xiàn)測(cè)試結(jié)果。通過(guò)ids傳入的參數(shù)可以對(duì)測(cè)試用例進(jìn)行說(shuō)明,這樣更方便我們查看測(cè)試結(jié)果。

2.5 name

fixture的別名。fixture的name默認(rèn)是@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()  #定義一個(gè)login并實(shí)例化SubClass
        login.sub_login() #調(diào)用SubClass中的sub_login()
        print('這是testcase1')

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

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

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

注意:

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

2.6 fixture_function

目前pytest官方文檔未給出具體說(shuō)明。

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

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

相關(guān)文章

最新評(píng)論