Pytest?Fixture參數(shù)講解及使用
Fixture參數(shù)詳解及使用
Fixture的調(diào)用方式:
@pytest.fixture(scope = "function",params=None,autouse=False,ids=None,name=None)
參數(shù)詳解:
1、SCOPE
用于控制Fixture的作用范圍
作用類似于Pytest的setup/teardown
默認取值為function(函數(shù)級別),控制范圍的排序為:session > module > class > function
取值 | 范圍 說明 |
---|---|
function | 函數(shù)級 每一個函數(shù)或方法都會調(diào)用 |
class | 函數(shù)級 模塊級 每一個.py文件調(diào)用一次 |
module | 模塊級 每一個.py文件調(diào)用一次 |
session | 會話級 每次會話只需要運行一次,會話內(nèi)所有方法及類,模塊都共享這個方法 |
作用范圍舉例:
scope = “function”
語法:
@pytest.fixture() #或者 @pytest.fixture(scope='function')
場景一:做為參數(shù)傳入
import pytest # fixture函數(shù)(類中) 作為多個參數(shù)傳入 @pytest.fixture() def login(): print("打開瀏覽器") 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傳入了兩個fixture") def test_004(self): print("004未傳入仍何fixture哦") if __name__ == '__main__': pytest.main()
運行結(jié)果:
從運行結(jié)果可以看出,fixture做為參數(shù)傳入時,會在執(zhí)行函數(shù)之前執(zhí)行該fixture函數(shù)。再將值傳入測試函數(shù)做為參數(shù)使用,這個場景多用于登錄
場景二、Fixture的相互調(diào)用
代碼:
import pytest # fixtrue作為參數(shù),互相調(diào)用傳入 @pytest.fixture() def account(): a = "account" print("第一層fixture") return a #Fixture的相互調(diào)用一定是要在測試類里調(diào)用這層fixture才會生次,普通函數(shù)單獨調(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()
運行結(jié)果:
注:
1.即使fixture之間支持相互調(diào)用,但普通函數(shù)直接使用fixture是不支持的,一定是在測試函數(shù)內(nèi)調(diào)用才會逐級調(diào)用生效
2.有多層fixture調(diào)用時,最先執(zhí)行的是最后一層fixture,而不是先執(zhí)行傳入測試函數(shù)的fixture
3.上層fixture的值不會自動return,這里就類似函數(shù)相互調(diào)用一樣的邏輯
scope = “class”:
**當測試類內(nèi)的每一個測試方法都調(diào)用了fixture,fixture只在該class下所有測試用例執(zhí)行前執(zhí)行一次
**測試類下面只有一些測試方法使用了fixture函數(shù)名,這樣的話,fixture只在該class下第一個使用fixture函數(shù)的測試用例位置開始算,后面所有的測試用例執(zhí)行前只執(zhí)行一次。而該位置之前的測試用例就不管。
語法
@pytest.fixture(scope='class')
場景一、
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()
運行結(jié)果:
場景二、
import pytest @pytest.fixture(scope='class') def login(): a = '123' print("輸入賬號密碼登陸") 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()
運行結(jié)果:
scope = “module”:與class相同,只從.py文件開始引用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()
運行結(jié)果:
scope = “session”:用法將在conftest.py文章內(nèi)詳細介紹
session的作用范圍是針對.py級別的,module是對當前.py生效,seesion是對多個.py文件生效
session只作用于一個.py文件時,作用相當于module
所以session多數(shù)與contest.py文件一起使用,做為全局Fixture
2、params:
Fixture的可選形參列表,支持列表傳入
默認None,每個param的值
fixture都會去調(diào)用執(zhí)行一次,類似for循環(huán)
可與參數(shù)ids一起使用,作為每個參數(shù)的標識,詳見ids
被Fixture裝飾的函數(shù)要調(diào)用是采用:Request.param(固定寫法,如下圖)
舉個栗子:
3、ids:
用例標識ID
與params配合使用,一對一關(guān)系
舉個栗子:
未配置ids之前,用例:
配置了IDS后:
4、autouse:
默認False
若為True,剛每個測試函數(shù)都會自動調(diào)用該fixture,無需傳入fixture函數(shù)名
由此我們可以總結(jié)出調(diào)用fixture的三種方式:
1.函數(shù)或類里面方法直接傳fixture的函數(shù)參數(shù)名稱
2.使用裝飾器@pytest.mark.usefixtures()修飾
3.autouse=True自動調(diào)用,無需傳仍何參數(shù),作用范圍跟著scope走(謹慎使用)
讓我們來看一下,當autouse=ture的效果:
5、Name:
fixture的重命名
通常來說使用 fixture 的測試函數(shù)會將 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ù)名稱,會失敗 def test_2(test_name): print("使用name參數(shù)后,仍傳入函數(shù)名稱,會失敗")
運行結(jié)果:
到此這篇關(guān)于Pytest之Fixture參數(shù)詳解及使用的文章就介紹到這了,更多相關(guān)Pytest Fixture使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于django channel實現(xiàn)websocket的聊天室的方法示例
這篇文章主要介紹了基于基于django channel實現(xiàn)websocket的聊天室的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04