日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

pytest使用parametrize將參數(shù)化變量傳遞到fixture_python

作者:把蘋果咬哭的測(cè)試筆記 ? 更新時(shí)間: 2022-07-28 編程語(yǔ)言

分享一個(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,注意名稱與fixture名稱一致。
  • indirect=True:作用是讓parametrize中的參數(shù)名稱,也就是"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依賴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í)行。

原文鏈接:https://blog.csdn.net/wessonlan/article/details/124813048

欄目分類
最近更新