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)在有小伙伴來(lái)需求了,她要在setup_before()里去造數(shù),通過(guò)請(qǐng)求另一個(gè)接口,這個(gè)請(qǐng)求也需要使用token。
那么,問(wèn)題也就可以轉(zhuǎn)化為:
- 要將case文件里的參數(shù),傳遞到fixture函數(shù)中。
- gen_token()里返回的值,setup_before()和test_case()里都要拿到。
二、使用@pytest.mark.parametrize、以及fixture的調(diào)用來(lái)解決
這里把實(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)傳過(guò)來(lái)的值相加等于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("跳過(guò)")
這里模擬了給測(cè)試用例造數(shù)據(jù)的fixture函數(shù),如果沒(méi)拿到token的話,就跳過(guò)測(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,來(lái)把參數(shù)傳遞給目標(biāo)fixture函數(shù),你希望把參數(shù)傳遞給哪個(gè)fixture函數(shù)就加哪個(gè)。比如這里的gen_token和setup_before,注意名稱(chēng)與fixture名稱(chēng)一致。
- indirect=True:作用是讓parametrize中的參數(shù)名稱(chēng),也就是"gen_token"當(dāng)成函數(shù)執(zhí)行,并且后面的參數(shù)值test_param,作為"gen_token"的傳參。
- request.param:接受傳參的fixture函數(shù),使用request.param來(lái)獲取值。
fixture調(diào)用fixture
fixture之間的相互調(diào)用,在之前的文章里已經(jīng)有過(guò)詳述了。既然這里setup_before依賴(lài)gen_token,之間傳遞調(diào)用即可setup_before(request, gen_token)。
在各環(huán)節(jié)做了些print打印出信息,幫助理解執(zhí)行過(guò)程。
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: 跳過(guò) 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)文章
pytorch超詳細(xì)安裝教程之Anaconda、PyTorch和PyCharm全套安裝流程
這篇文章主要介紹了pytorch超詳細(xì)安裝教程之Anaconda、PyTorch和PyCharm全套安裝流程,介紹基于Anaconda環(huán)境以及PyCharm軟件結(jié)合,安裝PyTorch深度學(xué)習(xí)框架,需要的朋友可以參考下2023-04-04Django項(xiàng)目之Elasticsearch搜索引擎的實(shí)例
今天小編就為大家分享一篇Django項(xiàng)目之Elasticsearch搜索引擎的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Python3多線程處理爬蟲(chóng)的實(shí)戰(zhàn)
本文主要介紹了Python3多線程處理爬蟲(chóng)的實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03python錯(cuò)誤提示:Errno?2]?No?such?file?or?directory的解決方法
我相信很多人在學(xué)習(xí)Python的時(shí)候,特別是在open文件的時(shí)候總還碰到,還報(bào)錯(cuò)IOError:[Errno?2]沒(méi)有這樣的文件或目錄:'E://aaa.txt',這篇文章主要給大家介紹了關(guān)于python錯(cuò)誤提示:Errno?2]?No?such?file?or?directory的解決方法,需要的朋友可以參考下2022-02-02python使用openpyxl庫(kù)處理Excel文件詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于python使用openpyxl庫(kù)處理Excel文件詳細(xì)教程的相關(guān)資料,openpyxl屬于第三方模塊,在python中用來(lái)處理excel文件,可以對(duì)excel進(jìn)行的操作有讀寫(xiě)、修改、調(diào)整樣式及插入圖片等,需要的朋友可以參考下2023-11-11python如何導(dǎo)出微信公眾號(hào)文章方法詳解
這篇文章主要介紹了python如何導(dǎo)出微信公眾號(hào)文章方法詳解,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08python 瀑布線指標(biāo)編寫(xiě)實(shí)例
這篇文章主要介紹了python 瀑布線指標(biāo)編寫(xiě)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06