Python中pytest的參數(shù)化實(shí)例解析
pytest的參數(shù)化
參數(shù)化多個(gè)參數(shù):
可以使用多個(gè)參數(shù)來(lái)參數(shù)化測(cè)試。例如:
import pytest
@pytest.mark.parametrize("x, y, expected", [
(1, 2, 3),
(3, 4, 7),
(5, 6, 11),
])
def test_addition(x, y, expected):
assert x + y == expected參數(shù)化列表:
可以使用列表來(lái)參數(shù)化測(cè)試。例如:
import pytest
@pytest.mark.parametrize("test_input, expected_output", [
([1, 2, 3], 6),
([4, 5, 6], 15),
([7, 8, 9], 24),
])
def test_sum(test_input, expected_output):
assert sum(test_input) == expected_output參數(shù)化字典:
可以使用字典來(lái)參數(shù)化測(cè)試。例如:
import pytest
@pytest.mark.parametrize("test_input, expected_output", [
({"x": 1, "y": 2}, 3),
({"x": 3, "y": 4}, 7),
({"x": 5, "y": 6}, 11),
])
def test_addition(test_input, expected_output):
assert test_input["x"] + test_input["y"] == expected_output參數(shù)化文件:
可以使用文件來(lái)參數(shù)化測(cè)試。例如:
import pytest
import csv
def read_csv():
with open('testdata.csv', 'r') as f:
reader = csv.reader(f)
rows = []
for row in reader:
rows.append(row)
return rows[1:]
@pytest.mark.parametrize("test_input, expected_output", read_csv())
def test_addition(test_input, expected_output):
x, y = map(int, test_input.split(','))
assert x + y == int(expected_output)動(dòng)態(tài)參數(shù)化:
可以使用 Python 代碼動(dòng)態(tài)生成參數(shù)。例如:
import pytest
import time
def get_test_data():
test_data = []
start_time = time.time()
while time.time() - start_time < 10: # 運(yùn)行時(shí)間小于 10 秒
x = random.randint(1, 100)
y = random.randint(1, 100)
expected = x + y
test_data.append((x, y, expected))
return test_data
@pytest.mark.parametrize("x, y, expected", get_test_data())
def test_addition(x, y, expected):
assert x + y == expected從外部數(shù)據(jù)源加載數(shù)據(jù):
可以使用動(dòng)態(tài)參數(shù)化從外部數(shù)據(jù)源加載測(cè)試數(shù)據(jù),例如數(shù)據(jù)庫(kù)、API 或其他 Web 服務(wù)。例如:
import pytest
import requests
def get_test_data():
response = requests.get('https://api.example.com/data')
test_data = []
for item in response.json():
x = item['x']
y = item['y']
expected = item['expected']
test_data.append((x, y, expected))
return test_data
@pytest.mark.parametrize("x, y, expected", get_test_data())
def test_addition(x, y, expected):
assert x + y == expected在上面的例子中,get_test_data 函數(shù)使用 requests 庫(kù)從遠(yuǎn)程 API 加載測(cè)試數(shù)據(jù),并返回一個(gè)測(cè)試數(shù)據(jù)列表。然后,使用 @pytest.mark.parametrize 裝飾器動(dòng)態(tài)參數(shù)化測(cè)試,使用從 API 加載的測(cè)試數(shù)據(jù)作為參數(shù)。
組合參數(shù):
可以使用 itertools 庫(kù)中的 product 函數(shù)生成參數(shù)的所有組合。例如:
import pytest
import itertools
@pytest.mark.parametrize("x, y", itertools.product([1, 2, 3], [4, 5, 6]))
def test_multiplication(x, y):
assert x * y == y * x在上面的例子中,使用 itertools.product 函數(shù)生成 x 和 y 的所有組合,并將它們作為參數(shù)傳遞給測(cè)試函數(shù)。
參數(shù)化生成器:
可以使用生成器函數(shù)生成參數(shù)。例如:
import pytest
import random
def get_test_data():
while True:
x = random.randint(1, 100)
y = random.randint(1, 100)
expected = x + y
yield (x, y, expected)
@pytest.mark.parametrize("x, y, expected", get_test_data())
def test_addition(x, y, expected):
assert x + y == expected到此這篇關(guān)于Python中pytest的參數(shù)化實(shí)例解析的文章就介紹到這了,更多相關(guān)pytest的參數(shù)化實(shí)例解析內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- pytest實(shí)戰(zhàn)技巧之參數(shù)化基本用法和多種方式
- pytest使用@pytest.mark.parametrize()實(shí)現(xiàn)參數(shù)化的示例代碼
- pytest?fixtures函數(shù)及測(cè)試函數(shù)的參數(shù)化解讀
- pytest使用parametrize將參數(shù)化變量傳遞到fixture
- 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 詳解爬取并統(tǒng)計(jì)CSDN全站熱榜標(biāo)題關(guān)鍵詞詞頻流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用Python爬取CSDN全站綜合熱榜標(biāo)題,順便統(tǒng)計(jì)關(guān)鍵詞詞頻,大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11
Python計(jì)算圖片數(shù)據(jù)集的均值方差示例詳解
這篇文章主要為大家介紹了Python計(jì)算圖片數(shù)據(jù)集的均值方差,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python的pywifi無(wú)線網(wǎng)絡(luò)庫(kù)的具體使用
pywifi是一個(gè)基于Python的用于操作無(wú)線網(wǎng)絡(luò)的庫(kù),本文就來(lái)介紹一下pywifi的安裝及實(shí)際應(yīng)用場(chǎng)景使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Python實(shí)用技巧之利用元組代替字典并為元組元素命名
這篇文章主要給大家介紹了關(guān)于Python實(shí)用技巧之利用元組代替字典并為元組元素命名的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-07-07
python實(shí)現(xiàn)基于信息增益的決策樹歸納
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)基于信息增益的決策樹歸納,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
python np.arange 步長(zhǎng)0.1的問題需要特別注意
這篇文章主要介紹了python np.arange 步長(zhǎng)0.1的問題需要特別注意,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
python實(shí)現(xiàn)定時(shí)任務(wù)的多種方式詳解
Python社區(qū)提供了多種庫(kù)來(lái)實(shí)現(xiàn)定時(shí)任務(wù),下面這篇文章主要給大家介紹了關(guān)于python實(shí)現(xiàn)定時(shí)任務(wù)的多種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-05-05

