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

pytest官方文檔解讀fixtures的autouse

 更新時(shí)間:2022年06月01日 14:18:52   作者:把蘋果咬哭的測試筆記  
這篇文章主要為大家介紹了pytest官方文檔解讀fixtures的autouse,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

現(xiàn)在我們已經(jīng)知道了,fixtures是一個(gè)非常強(qiáng)大的功能。

那么有的時(shí)候,我們可能會(huì)寫一個(gè)fixture,而這個(gè)fixture所有的測試函數(shù)都會(huì)用到它。

那這個(gè)時(shí)候,就可以用autouse自動(dòng)讓所有的測試函數(shù)都請求它,不需要在每個(gè)測試函數(shù)里顯示的請求一遍。

具體用法就是,將autouse=True傳遞給fixture的裝飾器即可。

import pytest
@pytest.fixture
def first_entry():
    return "a"
@pytest.fixture
def order(first_entry):
    return []
@pytest.fixture(autouse=True)
def append_first(order, first_entry):
    return order.append(first_entry)
def test_string_only(order, first_entry):
    assert order == [first_entry]
def test_string_and_int(order, first_entry):
    order.append(2)
    assert order == [first_entry, 2]

先來看第一個(gè)測試函數(shù)test_string_only(order, first_entry)的執(zhí)行情況:

  • 雖然在測試函數(shù)里請求了2個(gè)fixture函數(shù),但是order拿到的并不是[],first_entry拿到的也并不是"a"。
  • 因?yàn)榇嬖诹艘粋€(gè)autouse=True的fixture函數(shù),所以append_first先會(huì)被調(diào)用執(zhí)行。
  • 在執(zhí)行append_first過程中,又分別請求了order、 first_entry這2和fixture函數(shù)。
  • 接著,append_first對(duì)分別拿到的[]和"a"進(jìn)行append處理,最終返回了["a"]。所以,斷言assert order == [first_entry]是成功的。

同理,第二個(gè)測試函數(shù)test_string_and_int(order, first_entry)的執(zhí)行過程亦是如此。

以上就是pytest官方文檔解讀fixtures的autouse的詳細(xì)內(nèi)容,更多關(guān)于pytest解讀fixtures的autouse的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論