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)文章
python3 常見(jiàn)解密加密算法實(shí)例分析【base64、MD5等】
這篇文章主要介紹了python3 常見(jiàn)解密加密算法,結(jié)合實(shí)例形式分析了Python的base64模塊加密,以及基于pycrypto模塊的MD5加密等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12python用selenium打開(kāi)chrome瀏覽器保持登錄方式
大家好,本篇文章主要講的是python用selenium打開(kāi)chrome瀏覽器保持登錄方式,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下2022-02-02Python HTML解析模塊HTMLParser用法分析【爬蟲(chóng)工具】
這篇文章主要介紹了Python HTML解析模塊HTMLParser用法,結(jié)合實(shí)例形式分析了HTMLParser模塊功能、常用函數(shù)及作為爬蟲(chóng)工具相關(guān)使用技巧,需要的朋友可以參考下2019-04-04python解析發(fā)往本機(jī)的數(shù)據(jù)包示例 (解析數(shù)據(jù)包)
這篇文章主要介紹了使用python解析獲取發(fā)往本機(jī)的數(shù)據(jù)包,并打印出來(lái), 大家參考使用吧2014-01-01使用AJAX和Django獲取數(shù)據(jù)的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于使用AJAX和Django獲取數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10使用Python爬蟲(chóng)框架獲取HTML網(wǎng)頁(yè)中指定區(qū)域的數(shù)據(jù)
在當(dāng)今互聯(lián)網(wǎng)時(shí)代,數(shù)據(jù)已經(jīng)成為了一種寶貴的資源,無(wú)論是進(jìn)行市場(chǎng)分析、輿情監(jiān)控,還是進(jìn)行學(xué)術(shù)研究,獲取網(wǎng)頁(yè)中的數(shù)據(jù)都是一個(gè)非常重要的步驟,Python提供了多種爬蟲(chóng)框架來(lái)幫助我們高效地獲取網(wǎng)頁(yè)數(shù)據(jù),本文將詳細(xì)介紹如何使用Python爬蟲(chóng)框架來(lái)獲取HTML網(wǎng)頁(yè)中指定區(qū)域的數(shù)據(jù)2025-03-03Python實(shí)現(xiàn)聊天機(jī)器人的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)聊天機(jī)器人,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07python中將兩組數(shù)據(jù)放在一起按照某一固定順序shuffle的實(shí)例
今天小編就為大家分享一篇python中將兩組數(shù)據(jù)放在一起按照某一固定順序shuffle的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07