pytest官方文檔解讀fixtures調(diào)用fixtures及fixture復(fù)用性
fixtures調(diào)用其他fixtures及fixture復(fù)用性
pytest最大的優(yōu)點(diǎn)之一就是它非常靈活。
它可以將復(fù)雜的測試需求簡化為更簡單和有組織的函數(shù),然后這些函數(shù)可以根據(jù)自身的需求去依賴別的函數(shù)。
fixtures可以調(diào)用別的fixtures正是靈活性的體現(xiàn)之一。
一、Fixtures調(diào)用別的Fixtures
直接看一個簡單示例:
import pytest # Arrange @pytest.fixture def first_entry(): # 這是一個fixture函數(shù),返回值:"a" return "a" # Arrange @pytest.fixture def order(first_entry): # 這是另一個fixture函數(shù),請求了上一個fixture函數(shù)first_entry(), # 并且把first_entry()的返回值,放進(jìn)了列表[]里,最后返回 return [first_entry] def test_string(order): # Act # 測試函數(shù)中請求了第二個fixture函數(shù)order,可以拿到返回的[] order.append("b") # Assert assert order == ["a", "b"]
可以看到,pytest中的某個fixture請求別的fixture,就像測試函數(shù)請求fixture一樣,所有的請求規(guī)則都適用。
同樣,如果這些事情換我們自己來做的話,應(yīng)該是下面這樣子:
def first_entry(): return "a" def order(first_entry): return [first_entry] def test_string(order): # Act order.append("b") # Assert assert order == ["a", "b"] entry = first_entry() the_list = order(first_entry=entry) test_string(order=the_list)
二、Fixtures的復(fù)用性
pytest中的fixtures還可以讓我們像使用普通函數(shù)一樣,能夠定義反復(fù)重用的通用setup步驟。
兩個不同的測試函數(shù)可以請求相同的fixture,每個測試函數(shù)都會獲得該fixture的各自結(jié)果。
這樣的優(yōu)點(diǎn)就是,確保不同的測試函數(shù)之間不會相互影響。
我們可以使用這種機(jī)制來確保每個測試函數(shù)都獲得各自新的、干凈的、一致的數(shù)據(jù)。
import pytest # Arrange @pytest.fixture def first_entry(): return "a" # Arrange @pytest.fixture def order(first_entry): return [first_entry] def test_string(order): # Act order.append("b") # Assert assert order == ["a", "b"] def test_int(order): # Act order.append(2) # Assert assert order == ["a", 2]
從代碼可以看出,fixture函數(shù)order
雖然先后被兩個測試函數(shù)調(diào)用,但是每次被調(diào)用給出的結(jié)果都是一樣的。并不會因為在測試函數(shù)test_string
中,進(jìn)行了order.append("b")
后,就影響了order
在測試函數(shù)test_int
中的返回值。
同樣,這些事情換成我們自己來做,那就是這樣的:
def first_entry(): return "a" def order(first_entry): return [first_entry] def test_string(order): # Act order.append("b") # Assert assert order == ["a", "b"] def test_int(order): # Act order.append(2) # Assert assert order == ["a", 2] entry = first_entry() the_list = order(first_entry=entry) test_string(order=the_list) entry = first_entry() the_list = order(first_entry=entry) test_int(order=the_list)
接下來,繼續(xù)跟著官方文檔解讀fixtures的特點(diǎn):一次請求多個fixtures、fixtures被多次請求。
以上就是pytest官方文檔解讀fixtures調(diào)用fixtures及fixture復(fù)用性 的詳細(xì)內(nèi)容,更多關(guān)于pytest fixtures調(diào)用復(fù)用性的資料請關(guān)注腳本之家其它相關(guān)文章!
- pytest解讀fixtures中yield與addfinalizer區(qū)別
- pytest解讀fixtures之Teardown處理yield和addfinalizer方案
- pytest官方文檔解讀fixtures的調(diào)用方式
- pytest官方文檔解讀fixtures
- pytest官方文檔解讀fixtures的autouse
- pytest解讀一次請求多個fixtures及多次請求
- pytest解讀fixture有效性及跨文件共享fixtures
- pytest?fixtures函數(shù)及測試函數(shù)的參數(shù)化解讀
- pytest fixtures裝飾器的使用和如何控制用例的執(zhí)行順序
- Pytest中Fixtures的高級用法
相關(guān)文章
Pandas DataFrame中的tuple元素遍歷的實現(xiàn)
這篇文章主要介紹了Pandas DataFrame中的tuple元素遍歷的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10Python字符串的創(chuàng)建和駐留機(jī)制詳解
字符串駐留是一種在內(nèi)存中僅保存一份相同且不可變字符串的方法,本文重點(diǎn)給大家介紹Python字符串的創(chuàng)建和駐留機(jī)制,感興趣的朋友跟隨小編一起看看吧2022-02-02Ubuntu16安裝CUDA(9.1)和cuDNN的實現(xiàn)步驟(圖文)
本文主要介紹了Ubuntu16安裝CUDA(9.1)和cuDNN,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07Python3自帶工具2to3.py 轉(zhuǎn)換 Python2.x 代碼到Python3的操作
Python3自帶工具2to3.py 轉(zhuǎn)換 Python2.x 代碼到Python3的操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python計算三角函數(shù)之a(chǎn)sin()方法的使用
這篇文章主要介紹了Python計算三角函數(shù)之a(chǎn)sin()方法的使用,是Python入門的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05利用pyuic5將ui文件轉(zhuǎn)換為py文件的方法
今天小編就為大家分享一篇利用pyuic5將ui文件轉(zhuǎn)換為py文件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06