pytest解讀fixture有效性及跨文件共享fixtures
fixture有效性及跨文件共享fixtures
一、fixture有效性
fixture有效性,說白了就是fixture函數(shù)只有在它定義的使用范圍內(nèi),才可以被請求到。比如,在類里面定義了一個fixture,那么就只能是這個類中的測試函數(shù)才可以請求。但是,如果一個fixture定義的范圍是整個模塊,那么這個模塊下的每個測試函數(shù)都可以去請求。
這里還有另一個影響fixture有效性的參數(shù)autouse=True
,默認(rèn)為False,等于True的話會在其他fixture之前先執(zhí)行該fixture,后面有需要另起一篇,這里簡短帶過。
另外,一個fixture函數(shù)還可以請求任何其他的fixture函數(shù)。不管被請求的那個fixture函數(shù)在哪里定義,只要測試函數(shù)請求了它們,fixture函數(shù)就可以。
看示例代碼(為了更直觀的看效果,在官方代碼基礎(chǔ)上我加了幾個fixture函數(shù)的print):
# content of test_module1.py import pytest @pytest.fixture def order(): print("\n運(yùn)行fixture函數(shù)-order") return [] @pytest.fixture def outer(order, inner): print("運(yùn)行fixture函數(shù)-outer") order.append("outer") class TestOne: @pytest.fixture def inner(self, order): print("運(yùn)行TestOne下的fixture-inner") order.append("one") def test_order(self, order, outer): assert order == ["one", "outer"] class TestTwo: @pytest.fixture def inner(self, order): print("運(yùn)行TestTwo下的fixture-inner") order.append("two") def test_order(self, order, outer): assert order == ["two", "outer"]
注意:
- 這里有一個fixture函數(shù)
outer
在測試類的外部 - 另外還有2個名字都叫
inner
的fixture函數(shù),分別在測試類TestOne
和TestTwo
中。 - 在外部的fixture函數(shù)
outer
中,又請求了內(nèi)部的fixture函數(shù)inner
。
現(xiàn)在我只運(yùn)行類TestOne
,看運(yùn)行結(jié)果:
test_module1.py 運(yùn)行fixture函數(shù)-order 運(yùn)行TestOne下的fixture-inner 運(yùn)行fixture函數(shù)-outer . [100%] ============================== 1 passed in 0.01s ============================== Process finished with exit code 0
說明測試函數(shù)里的斷言通過。測試函數(shù)執(zhí)行的時候,外部outer
請求的inner
是TestOne
下的。雖然TestOne
類下的inner
,只能作用于TestOne
下的測試函數(shù)。但是,由于測試函數(shù)請求了外部的outer
,所以,外部的outer
也就可以請到內(nèi)部的inner
。
官方還給出一個示意圖,可以結(jié)合著上述的思路,理解一下。
注意,fixture定義的范圍與它將被實(shí)例化的順序無關(guān):實(shí)例化順序由調(diào)用邏輯強(qiáng)制執(zhí)行
可以參考 http://www.dbjr.com.cn/article/250148.htm
二、跨文件共享fixtures
如果你把fixture函數(shù)放到conftest.py
文件中,那么在這個文件所在的整個目錄下,都可以直接請求里面的fixture,不需要導(dǎo)入。
在實(shí)際場景中,我們的測試目錄或者包可能有多層的嵌套,這種情況下,每個目錄都可以有一個自己的conftest文件。比如,像這樣:
各層級里的內(nèi)容是這樣的:
tests/ __init__.py conftest.py # content of tests/conftest.py import pytest @pytest.fixture def order(): return [] @pytest.fixture def top(order, innermost): order.append("top") test_top.py # content of tests/test_top.py import pytest @pytest.fixture def innermost(order): order.append("innermost top") def test_order(order, top): assert order == ["innermost top", "top"] subpackage/ __init__.py conftest.py # content of tests/subpackage/conftest.py import pytest @pytest.fixture def mid(order): order.append("mid subpackage") test_subpackage.py # content of tests/subpackage/test_subpackage.py import pytest @pytest.fixture def innermost(order, mid): order.append("innermost subpackage") def test_order(order, top): assert order == ["mid subpackage", "innermost subpackage", "top"]
同樣的,這里也有一張作用域邊界圖幫助理解。
知識點(diǎn):
- 頂層下的conftest里的order和top對當(dāng)前層和下層級的所有可用(一個圈就對應(yīng)各自的作用域)。
- 測試函數(shù)只可以向上層級搜索可用的fixture函數(shù)(出圈),但是出圈查找的過程中,不能再進(jìn)到別的圈子向下查找。所以,tests/subpackage/test_subpackage.py::test_order可以找到定義在tests/subpackage/test_subpackage.py里的innermost。但是,另一個定義在tests/test_top.py中,名字也叫innermost的fixture,對test_order來說就不可用了。
其實(shí)對于上述,按照我的白話來說,想用conftest里的fixture函數(shù),你只能用同層級或者上層級的。但是上級里的其他兄弟目錄或者包,以及他們的下層級的conftest,你是不能用的。
但是讀了官方文檔,我覺得官方的那個圈子描述挺不錯的,更嚴(yán)謹(jǐn)。
以上就是pytest解讀fixture有效性及跨文件共享fixtures的詳細(xì)內(nèi)容,更多關(guān)于 pytest解讀fixture fixtures的資料請關(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官方文檔解讀fixtures調(diào)用fixtures及fixture復(fù)用性
- pytest?fixtures函數(shù)及測試函數(shù)的參數(shù)化解讀
- pytest fixtures裝飾器的使用和如何控制用例的執(zhí)行順序
- Pytest中Fixtures的高級用法
相關(guān)文章
Python實(shí)現(xiàn)線性搜索算法的示例代碼
線性搜索算法,也稱為順序搜索算法,是一種簡單但常用的搜索技術(shù),在本文中,將深入研究線性搜索算法,并演示如何在?Python?中實(shí)現(xiàn)它,需要的可以參考下2024-02-02python PyQt5對象類型的判定及對象刪除操作詳細(xì)解讀
PyQt5主要是用來判定一個對象的類型,或者說是否繼承自某個類,本文給大家介紹python PyQt5對象類型的判定,對象刪除操作詳細(xì)解讀,感興趣的朋友一起看看吧2024-07-07Python BentoML構(gòu)建部署和管理機(jī)器學(xué)習(xí)模型技巧掌握
BentoML是一個開源的Python框架,旨在簡化機(jī)器學(xué)習(xí)模型的打包、部署和管理,本文將深入介紹BentoML的功能和用法,提供詳細(xì)的示例代碼和解釋,幫助你更好地理解和應(yīng)用這個強(qiáng)大的工具2024-01-01pandas中concat函數(shù)實(shí)現(xiàn)橫向連接
在pandas中,concat函數(shù)可用于合并不同的Series和DataFrame對象,本文主要介紹了pandas中concat函數(shù)實(shí)現(xiàn)橫向連接,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04