Pytest?Fixture參數(shù)講解及使用
Fixture參數(shù)詳解及使用
Fixture的調(diào)用方式:
@pytest.fixture(scope = "function",params=None,autouse=False,ids=None,name=None)
參數(shù)詳解:
1、SCOPE
用于控制Fixture的作用范圍
作用類(lèi)似于Pytest的setup/teardown
默認(rèn)取值為function(函數(shù)級(jí)別),控制范圍的排序?yàn)椋簊ession > module > class > function
取值 | 范圍 說(shuō)明 |
---|---|
function | 函數(shù)級(jí) 每一個(gè)函數(shù)或方法都會(huì)調(diào)用 |
class | 函數(shù)級(jí) 模塊級(jí) 每一個(gè).py文件調(diào)用一次 |
module | 模塊級(jí) 每一個(gè).py文件調(diào)用一次 |
session | 會(huì)話(huà)級(jí) 每次會(huì)話(huà)只需要運(yùn)行一次,會(huì)話(huà)內(nèi)所有方法及類(lèi),模塊都共享這個(gè)方法 |
作用范圍舉例:
scope = “function”
語(yǔ)法:
@pytest.fixture() #或者 @pytest.fixture(scope='function')
場(chǎng)景一:做為參數(shù)傳入
import pytest # fixture函數(shù)(類(lèi)中) 作為多個(gè)參數(shù)傳入 @pytest.fixture() def login(): print("打開(kāi)瀏覽器") a = "account" return a @pytest.fixture() def logout(): print("關(guān)閉瀏覽器") class TestLogin: #傳入lonin fixture def test_001(self, login): print("001傳入了loging fixture") assert login == "account" #傳入logout fixture def test_002(self, logout): print("002傳入了logout fixture") def test_003(self, login, logout): print("003傳入了兩個(gè)fixture") def test_004(self): print("004未傳入仍何fixture哦") if __name__ == '__main__': pytest.main()
運(yùn)行結(jié)果:
從運(yùn)行結(jié)果可以看出,fixture做為參數(shù)傳入時(shí),會(huì)在執(zhí)行函數(shù)之前執(zhí)行該fixture函數(shù)。再將值傳入測(cè)試函數(shù)做為參數(shù)使用,這個(gè)場(chǎng)景多用于登錄
場(chǎng)景二、Fixture的相互調(diào)用
代碼:
import pytest # fixtrue作為參數(shù),互相調(diào)用傳入 @pytest.fixture() def account(): a = "account" print("第一層fixture") return a #Fixture的相互調(diào)用一定是要在測(cè)試類(lèi)里調(diào)用這層fixture才會(huì)生次,普通函數(shù)單獨(dú)調(diào)用是不生效的 @pytest.fixture() def login(account): print("第二層fixture") class TestLogin: def test_1(self, login): print("直接使用第二層fixture,返回值為{}".format(login)) def test_2(self, account): print("只調(diào)用account fixture,返回值為{}".format(account)) if __name__ == '__main__': pytest.main()
運(yùn)行結(jié)果:
注:
1.即使fixture之間支持相互調(diào)用,但普通函數(shù)直接使用fixture是不支持的,一定是在測(cè)試函數(shù)內(nèi)調(diào)用才會(huì)逐級(jí)調(diào)用生效
2.有多層fixture調(diào)用時(shí),最先執(zhí)行的是最后一層fixture,而不是先執(zhí)行傳入測(cè)試函數(shù)的fixture
3.上層fixture的值不會(huì)自動(dòng)return,這里就類(lèi)似函數(shù)相互調(diào)用一樣的邏輯
scope = “class”:
**當(dāng)測(cè)試類(lèi)內(nèi)的每一個(gè)測(cè)試方法都調(diào)用了fixture,fixture只在該class下所有測(cè)試用例執(zhí)行前執(zhí)行一次
**測(cè)試類(lèi)下面只有一些測(cè)試方法使用了fixture函數(shù)名,這樣的話(huà),fixture只在該class下第一個(gè)使用fixture函數(shù)的測(cè)試用例位置開(kāi)始算,后面所有的測(cè)試用例執(zhí)行前只執(zhí)行一次。而該位置之前的測(cè)試用例就不管。
語(yǔ)法
@pytest.fixture(scope='class')
場(chǎng)景一、
import pytest # fixture作用域 scope = 'class' @pytest.fixture(scope='class') def login(): print("scope為class") class TestLogin: def test_1(self, login): print("用例1") def test_2(self, login): print("用例2") def test_3(self, login): print("用例3") if __name__ == '__main__': pytest.main()
運(yùn)行結(jié)果:
場(chǎng)景二、
import pytest @pytest.fixture(scope='class') def login(): a = '123' print("輸入賬號(hào)密碼登陸") class TestLogin: def test_1(self): print("用例1") def test_2(self, login): print("用例2") def test_3(self, login): print("用例3") def test_4(self): print("用例4") if __name__ == '__main__': pytest.main()
運(yùn)行結(jié)果:
scope = “module”:與class相同,只從.py文件開(kāi)始引用fixture的位置生效
import pytest # fixture scope = 'module' @pytest.fixture(scope='module') def login(): print("fixture范圍為module") def test_01(): print("用例01") def test_02(login): print("用例02") class TestLogin(): def test_1(self): print("用例1") def test_2(self): print("用例2") def test_3(self): print("用例3") if __name__ == '__main__': pytest.main()
運(yùn)行結(jié)果:
scope = “session”:用法將在conftest.py文章內(nèi)詳細(xì)介紹
session的作用范圍是針對(duì).py級(jí)別的,module是對(duì)當(dāng)前.py生效,seesion是對(duì)多個(gè).py文件生效
session只作用于一個(gè).py文件時(shí),作用相當(dāng)于module
所以session多數(shù)與contest.py文件一起使用,做為全局Fixture
2、params:
Fixture的可選形參列表,支持列表傳入
默認(rèn)None,每個(gè)param的值
fixture都會(huì)去調(diào)用執(zhí)行一次,類(lèi)似for循環(huán)
可與參數(shù)ids一起使用,作為每個(gè)參數(shù)的標(biāo)識(shí),詳見(jiàn)ids
被Fixture裝飾的函數(shù)要調(diào)用是采用:Request.param(固定寫(xiě)法,如下圖)
舉個(gè)栗子:
3、ids:
用例標(biāo)識(shí)ID
與params配合使用,一對(duì)一關(guān)系
舉個(gè)栗子:
未配置ids之前,用例:
配置了IDS后:
4、autouse:
默認(rèn)False
若為T(mén)rue,剛每個(gè)測(cè)試函數(shù)都會(huì)自動(dòng)調(diào)用該fixture,無(wú)需傳入fixture函數(shù)名
由此我們可以總結(jié)出調(diào)用fixture的三種方式:
1.函數(shù)或類(lèi)里面方法直接傳fixture的函數(shù)參數(shù)名稱(chēng)
2.使用裝飾器@pytest.mark.usefixtures()修飾
3.autouse=True自動(dòng)調(diào)用,無(wú)需傳仍何參數(shù),作用范圍跟著scope走(謹(jǐn)慎使用)
讓我們來(lái)看一下,當(dāng)autouse=ture的效果:
5、Name:
fixture的重命名
通常來(lái)說(shuō)使用 fixture 的測(cè)試函數(shù)會(huì)將 fixture 的函數(shù)名作為參數(shù)傳遞,但是 pytest 也允許將fixture重命名
如果使用了name,那只能將name傳如,函數(shù)名不再生效
調(diào)用方法:@pytest.mark.usefixtures(‘fixture1’,‘fixture2’)
舉栗:
import pytest @pytest.fixture(name="new_fixture") def test_name(): pass #使用name參數(shù)后,傳入重命名函數(shù),執(zhí)行成功 def test_1(new_fixture): print("使用name參數(shù)后,傳入重命名函數(shù),執(zhí)行成功") #使用name參數(shù)后,仍傳入函數(shù)名稱(chēng),會(huì)失敗 def test_2(test_name): print("使用name參數(shù)后,仍傳入函數(shù)名稱(chēng),會(huì)失敗")
運(yùn)行結(jié)果:
到此這篇關(guān)于Pytest之Fixture參數(shù)詳解及使用的文章就介紹到這了,更多相關(guān)Pytest Fixture使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中Pytest測(cè)試框架的fixture使用詳解
- pytest內(nèi)置fixture使用臨時(shí)目錄流程詳解
- pytest使用parametrize將參數(shù)化變量傳遞到fixture
- pytest接口測(cè)試之fixture傳參數(shù)request的使用
- pytest fixtures裝飾器的使用和如何控制用例的執(zhí)行順序
- Pytest框架之fixture的詳細(xì)使用教程
- python+pytest接口自動(dòng)化之token關(guān)聯(lián)登錄的實(shí)現(xiàn)
- Pytest使用fixture實(shí)現(xiàn)token共享的方法
相關(guān)文章
Flask運(yùn)用Xterm實(shí)現(xiàn)交互終端的示例詳解
Xterm是一個(gè)基于X Window System的終端仿真器(Terminal Emulator),Xterm最初由MIT開(kāi)發(fā),它允許用戶(hù)在X Window環(huán)境下運(yùn)行文本終端程序,本文給大家介紹了Flask運(yùn)用Xterm實(shí)現(xiàn)交互終端的示例詳解,文中有詳細(xì)的代碼講解,需要的朋友可以參考下2023-11-11python實(shí)現(xiàn)kmp算法的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)kmp算法的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04python按照l(shuí)ist中字典的某key去重的示例代碼
這篇文章主要介紹了python按照l(shuí)ist中字典的某key去重的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10基于django channel實(shí)現(xiàn)websocket的聊天室的方法示例
這篇文章主要介紹了基于基于django channel實(shí)現(xiàn)websocket的聊天室的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04