pytest使用parametrize將參數(shù)化變量傳遞到fixture
分享一個(gè)關(guān)于在pytest中,如何將測(cè)試用例文件中的變量傳遞到fixture函數(shù)。
一、交代應(yīng)用場(chǎng)景
- 目前組內(nèi)的項(xiàng)目,在根目錄下是有一個(gè)conftest.py文件的,這里有個(gè)生成api token的fixture函數(shù),就叫它gen_token()吧。
- 每個(gè)case包下,也會(huì)有個(gè)conftest.py,用于存放適用于本模塊下測(cè)試用例的fixture函數(shù),比如有個(gè)叫setup_before()。
- 因?yàn)槟胻oken是請(qǐng)求接口的前提,所以在case里,比如有個(gè)test_case()里,要傳頂層的fixture函數(shù),也就是這樣test_case(gen_token)。
- 頂層的gen_token(),是需要3個(gè)傳參的。因?yàn)椴煌琧ase可能涉及到的生成不同用戶的token,所以我們把這個(gè)參數(shù)放在了case文件里。
ok,大背景是這樣的。
現(xiàn)在有小伙伴來需求了,她要在setup_before()里去造數(shù),通過請(qǐng)求另一個(gè)接口,這個(gè)請(qǐng)求也需要使用token。
那么,問題也就可以轉(zhuǎn)化為:
- 要將case文件里的參數(shù),傳遞到fixture函數(shù)中。
- gen_token()里返回的值,setup_before()和test_case()里都要拿到。
二、使用@pytest.mark.parametrize、以及fixture的調(diào)用來解決
這里把實(shí)際代碼抽象一下,轉(zhuǎn)化為簡(jiǎn)易代碼,方便演示和理解:
# 目錄結(jié)構(gòu)
-- /demo_top
-- /demo_sub
__init__.py
conftest.py
test_case.py
__init__.py
conftest.py以下分別是/demo_top/conftest.py、/demo_top/demo_sub/conftest.py、/demo_top/demo_sub/test_case.py的內(nèi)容。
1. /demo_top/conftest.py
# content of /demo_top/conftest.py
import pytest
@pytest.fixture()
def gen_token(request):
params = request.param
print("\n根目錄下gen_token()拿到的參數(shù):", params)
if params[0] + params[1] == 5:
return "api_token"
else:
return None這里,模擬生成token的fixture函數(shù),當(dāng)傳過來的值相加等于5,就會(huì)返回"api_token",否則返回None。
2. /demo_top/demo_sub/conftest.py
# content of /demo_top/demo_sub/conftest.py
import pytest
@pytest.fixture()
def setup_before(request, gen_token):
print("執(zhí)行子級(jí)setup_before,拿到的傳參:", request.param)
print("執(zhí)行子級(jí)setup_before,拿到gen_token的返回值:", gen_token)
if gen_token:
yield "造數(shù)完成"
print("測(cè)試用例test_case執(zhí)行完畢,清理測(cè)試數(shù)據(jù)")
else:
pytest.skip("跳過")這里模擬了給測(cè)試用例造數(shù)據(jù)的fixture函數(shù),如果沒拿到token的話,就跳過測(cè)試用例。
3. /demo_top/demo_sub/test_case.py
# content of /demo_top/demo_sub/test_case.py
import pytest
test_param = [(1, 4)]
@pytest.mark.parametrize("gen_token", test_param, indirect=True)
@pytest.mark.parametrize("setup_before", test_param, indirect=True)
def test_case1(gen_token, setup_before):
print("\n測(cè)試用例里拿到的gen_token返回值:", gen_token)
print("測(cè)試用例里拿到的setup_before返回值:", setup_before)
print("執(zhí)行測(cè)試用例test_case1...")
if __name__ == '__main__':
pytest.main(['-s', 'test_case.py'])這是測(cè)試用例文件了,里面有個(gè)測(cè)試函數(shù)test_case1,因?yàn)樗枰玫?個(gè)fixture函數(shù)返回的值,所以gen_token, setup_before都請(qǐng)求。
參數(shù)傳遞
- @pytest.mark.parametrize:使用pytest內(nèi)置的parametrize,來把參數(shù)傳遞給目標(biāo)fixture函數(shù),你希望把參數(shù)傳遞給哪個(gè)fixture函數(shù)就加哪個(gè)。比如這里的gen_token和setup_before,注意名稱與fixture名稱一致。
- indirect=True:作用是讓parametrize中的參數(shù)名稱,也就是"gen_token"當(dāng)成函數(shù)執(zhí)行,并且后面的參數(shù)值test_param,作為"gen_token"的傳參。
- request.param:接受傳參的fixture函數(shù),使用request.param來獲取值。
fixture調(diào)用fixture
fixture之間的相互調(diào)用,在之前的文章里已經(jīng)有過詳述了。既然這里setup_before依賴gen_token,之間傳遞調(diào)用即可setup_before(request, gen_token)。
在各環(huán)節(jié)做了些print打印出信息,幫助理解執(zhí)行過程。
test_case.py [100%] ============================== 1 passed in 0.08s ============================== 根目錄下gen_token()拿到的參數(shù): (1, 4) 執(zhí)行子級(jí)setup_before,拿到的傳參: (1, 4) 執(zhí)行子級(jí)setup_before,拿到gen_token的返回值: api_token . 測(cè)試用例里拿到的gen_token返回值: api_token 執(zhí)行測(cè)試用例test_case1... 測(cè)試用例test_case執(zhí)行完畢,清理測(cè)試數(shù)據(jù) Process finished with exit code 0
再看下gen_token不返回token的情況,改下傳參test_param = [(2, 4)]。
test_case.py [100%] ============================= 1 skipped in 0.08s ==============================s 根目錄下gen_token()拿到的參數(shù): (2, 4) 執(zhí)行子級(jí)setup_before,拿到的傳參: (2, 4) 執(zhí)行子級(jí)setup_before,拿到gen_token的返回值: None Skipped: 跳過 Process finished with exit code 0
測(cè)試用例不執(zhí)行。
以上就是pytest使用parametrize將參數(shù)化變量傳遞到fixture的詳細(xì)內(nèi)容,更多關(guān)于pytest parametrize變量傳遞fixture的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- pytest實(shí)戰(zhàn)技巧之參數(shù)化基本用法和多種方式
- Python中pytest的參數(shù)化實(shí)例解析
- pytest使用@pytest.mark.parametrize()實(shí)現(xiàn)參數(shù)化的示例代碼
- pytest?fixtures函數(shù)及測(cè)試函數(shù)的參數(shù)化解讀
- Python基礎(chǔ)教程之pytest參數(shù)化詳解
- pytest實(shí)現(xiàn)測(cè)試用例參數(shù)化
- Pytest單元測(cè)試框架如何實(shí)現(xiàn)參數(shù)化
- Pytest參數(shù)化parametrize使用代碼實(shí)例
- pytest參數(shù)化:@pytest.mark.parametrize詳解
相關(guān)文章
python?HTTP協(xié)議相關(guān)庫requests urllib基礎(chǔ)學(xué)習(xí)
這篇文章主要介紹了python?HTTP協(xié)議相關(guān)庫requests urllib基礎(chǔ)學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Python中反轉(zhuǎn)二維數(shù)組的行和列問題
這篇文章主要介紹了Python中反轉(zhuǎn)二維數(shù)組的行和列問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Python實(shí)現(xiàn)從訂閱源下載圖片的方法
這篇文章主要介紹了Python實(shí)現(xiàn)從訂閱源下載圖片的方法,涉及Python采集的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
Python爬蟲爬取一個(gè)網(wǎng)頁上的圖片地址實(shí)例代碼
這篇文章主要介紹了Python爬蟲爬取一個(gè)網(wǎng)頁上的圖片地址實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Python如何使用Gitlab API實(shí)現(xiàn)批量的合并分支
這篇文章主要介紹了Python如何使用Gitlab API實(shí)現(xiàn)批量的合并分支,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
解決Shell執(zhí)行python文件,傳參空格引起的問題
今天小編就為大家分享一篇解決Shell執(zhí)行python文件,傳參空格引起的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python?常用模塊threading和Thread模塊之線程池
這篇文章主要介紹了Python?threading和Thread模塊之線程池,線程池如消費(fèi)者,負(fù)責(zé)接收任務(wù),并將任務(wù)分配到一個(gè)空閑的線程中去執(zhí)行。并不關(guān)心是哪一個(gè)線程執(zhí)行的這個(gè)任務(wù),具體介紹需要的小伙伴可以參考下面文章詳細(xì)內(nèi)容2022-06-06

