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
默認(rèn)取值為function(函數(shù)級別),控制范圍的排序?yàn)椋簊ession > module > class > function
| 取值 | 范圍 說明 |
|---|---|
| function | 函數(shù)級 每一個(gè)函數(shù)或方法都會調(diào)用 |
| class | 函數(shù)級 模塊級 每一個(gè).py文件調(diào)用一次 |
| module | 模塊級 每一個(gè).py文件調(diào)用一次 |
| session | 會話級 每次會話只需要運(yùn)行一次,會話內(nèi)所有方法及類,模塊都共享這個(gè)方法 |
作用范圍舉例:
scope = “function”
語法:
@pytest.fixture() #或者 @pytest.fixture(scope='function')
場景一:做為參數(shù)傳入
import pytest
# fixture函數(shù)(類中) 作為多個(gè)參數(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傳入了兩個(gè)fixture")
def test_004(self):
print("004未傳入仍何fixture哦")
if __name__ == '__main__':
pytest.main()運(yùn)行結(jié)果:

從運(yùn)行結(jié)果可以看出,fixture做為參數(shù)傳入時(shí),會在執(zhí)行函數(shù)之前執(zhí)行該fixture函數(shù)。再將值傳入測試函數(shù)做為參數(shù)使用,這個(gè)場景多用于登錄
場景二、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ù)單獨(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是不支持的,一定是在測試函數(shù)內(nèi)調(diào)用才會逐級調(diào)用生效
2.有多層fixture調(diào)用時(shí),最先執(zhí)行的是最后一層fixture,而不是先執(zhí)行傳入測試函數(shù)的fixture
3.上層fixture的值不會自動(dòng)return,這里就類似函數(shù)相互調(diào)用一樣的邏輯
scope = “class”:
**當(dāng)測試類內(nèi)的每一個(gè)測試方法都調(diào)用了fixture,fixture只在該class下所有測試用例執(zhí)行前執(zhí)行一次
**測試類下面只有一些測試方法使用了fixture函數(shù)名,這樣的話,fixture只在該class下第一個(gè)使用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()運(yùn)行結(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()運(yùn)行結(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()運(yùn)行結(jié)果:

scope = “session”:用法將在conftest.py文章內(nèi)詳細(xì)介紹
session的作用范圍是針對.py級別的,module是對當(dāng)前.py生效,seesion是對多個(gè).py文件生效
session只作用于一個(gè).py文件時(shí),作用相當(dāng)于module
所以session多數(shù)與contest.py文件一起使用,做為全局Fixture
2、params:
Fixture的可選形參列表,支持列表傳入
默認(rèn)None,每個(gè)param的值
fixture都會去調(diào)用執(zhí)行一次,類似for循環(huán)
可與參數(shù)ids一起使用,作為每個(gè)參數(shù)的標(biāo)識,詳見ids
被Fixture裝飾的函數(shù)要調(diào)用是采用:Request.param(固定寫法,如下圖)
舉個(gè)栗子:

3、ids:
用例標(biāo)識ID
與params配合使用,一對一關(guān)系
舉個(gè)栗子:
未配置ids之前,用例:

配置了IDS后:

4、autouse:
默認(rèn)False
若為True,剛每個(gè)測試函數(shù)都會自動(dòng)調(diào)用該fixture,無需傳入fixture函數(shù)名
由此我們可以總結(jié)出調(diào)用fixture的三種方式:
1.函數(shù)或類里面方法直接傳fixture的函數(shù)參數(shù)名稱
2.使用裝飾器@pytest.mark.usefixtures()修飾
3.autouse=True自動(dòng)調(diào)用,無需傳仍何參數(shù),作用范圍跟著scope走(謹(jǐn)慎使用)
讓我們來看一下,當(dāng)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ù)名稱,會失敗")運(yùn)行結(jié)果:

到此這篇關(guān)于Pytest之Fixture參數(shù)詳解及使用的文章就介紹到這了,更多相關(guān)Pytest Fixture使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中Pytest測試框架的fixture使用詳解
- pytest內(nèi)置fixture使用臨時(shí)目錄流程詳解
- pytest使用parametrize將參數(shù)化變量傳遞到fixture
- pytest接口測試之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開發(fā),它允許用戶在X Window環(huán)境下運(yùn)行文本終端程序,本文給大家介紹了Flask運(yùn)用Xterm實(shí)現(xiàn)交互終端的示例詳解,文中有詳細(xì)的代碼講解,需要的朋友可以參考下2023-11-11
python實(shí)現(xiàn)kmp算法的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)kmp算法的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
基于django channel實(shí)現(xiàn)websocket的聊天室的方法示例
這篇文章主要介紹了基于基于django channel實(shí)現(xiàn)websocket的聊天室的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04

