pytest使用parametrize將參數化變量傳遞到fixture
分享一個關于在pytest中,如何將測試用例文件中的變量傳遞到fixture函數。
一、交代應用場景
- 目前組內的項目,在根目錄下是有一個conftest.py文件的,這里有個生成api token的fixture函數,就叫它gen_token()吧。
- 每個case包下,也會有個conftest.py,用于存放適用于本模塊下測試用例的fixture函數,比如有個叫setup_before()。
- 因為拿token是請求接口的前提,所以在case里,比如有個test_case()里,要傳頂層的fixture函數,也就是這樣test_case(gen_token)。
- 頂層的gen_token(),是需要3個傳參的。因為不同case可能涉及到的生成不同用戶的token,所以我們把這個參數放在了case文件里。
ok,大背景是這樣的。
現在有小伙伴來需求了,她要在setup_before()里去造數,通過請求另一個接口,這個請求也需要使用token。
那么,問題也就可以轉化為:
- 要將case文件里的參數,傳遞到fixture函數中。
- gen_token()里返回的值,setup_before()和test_case()里都要拿到。
二、使用@pytest.mark.parametrize、以及fixture的調用來解決
這里把實際代碼抽象一下,轉化為簡易代碼,方便演示和理解:
# 目錄結構
-- /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的內容。
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()拿到的參數:", params)
if params[0] + params[1] == 5:
return "api_token"
else:
return None這里,模擬生成token的fixture函數,當傳過來的值相加等于5,就會返回"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í)行子級setup_before,拿到的傳參:", request.param)
print("執(zhí)行子級setup_before,拿到gen_token的返回值:", gen_token)
if gen_token:
yield "造數完成"
print("測試用例test_case執(zhí)行完畢,清理測試數據")
else:
pytest.skip("跳過")這里模擬了給測試用例造數據的fixture函數,如果沒拿到token的話,就跳過測試用例。
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測試用例里拿到的gen_token返回值:", gen_token)
print("測試用例里拿到的setup_before返回值:", setup_before)
print("執(zhí)行測試用例test_case1...")
if __name__ == '__main__':
pytest.main(['-s', 'test_case.py'])這是測試用例文件了,里面有個測試函數test_case1,因為它需要用到2個fixture函數返回的值,所以gen_token, setup_before都請求。
參數傳遞
- @pytest.mark.parametrize:使用pytest內置的parametrize,來把參數傳遞給目標fixture函數,你希望把參數傳遞給哪個fixture函數就加哪個。比如這里的gen_token和setup_before,注意名稱與fixture名稱一致。
- indirect=True:作用是讓parametrize中的參數名稱,也就是"gen_token"當成函數執(zhí)行,并且后面的參數值test_param,作為"gen_token"的傳參。
- request.param:接受傳參的fixture函數,使用request.param來獲取值。
fixture調用fixture
fixture之間的相互調用,在之前的文章里已經有過詳述了。既然這里setup_before依賴gen_token,之間傳遞調用即可setup_before(request, gen_token)。
在各環(huán)節(jié)做了些print打印出信息,幫助理解執(zhí)行過程。
test_case.py [100%] ============================== 1 passed in 0.08s ============================== 根目錄下gen_token()拿到的參數: (1, 4) 執(zhí)行子級setup_before,拿到的傳參: (1, 4) 執(zhí)行子級setup_before,拿到gen_token的返回值: api_token . 測試用例里拿到的gen_token返回值: api_token 執(zhí)行測試用例test_case1... 測試用例test_case執(zhí)行完畢,清理測試數據 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()拿到的參數: (2, 4) 執(zhí)行子級setup_before,拿到的傳參: (2, 4) 執(zhí)行子級setup_before,拿到gen_token的返回值: None Skipped: 跳過 Process finished with exit code 0
測試用例不執(zhí)行。
以上就是pytest使用parametrize將參數化變量傳遞到fixture的詳細內容,更多關于pytest parametrize變量傳遞fixture的資料請關注腳本之家其它相關文章!
相關文章
python?HTTP協(xié)議相關庫requests urllib基礎學習
這篇文章主要介紹了python?HTTP協(xié)議相關庫requests urllib基礎學習,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
解決Shell執(zhí)行python文件,傳參空格引起的問題
今天小編就為大家分享一篇解決Shell執(zhí)行python文件,傳參空格引起的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python?常用模塊threading和Thread模塊之線程池
這篇文章主要介紹了Python?threading和Thread模塊之線程池,線程池如消費者,負責接收任務,并將任務分配到一個空閑的線程中去執(zhí)行。并不關心是哪一個線程執(zhí)行的這個任務,具體介紹需要的小伙伴可以參考下面文章詳細內容2022-06-06

