Python中pytest的參數(shù)化實例解析
pytest的參數(shù)化
參數(shù)化多個參數(shù):
可以使用多個參數(shù)來參數(shù)化測試。例如:
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ù)化列表:
可以使用列表來參數(shù)化測試。例如:
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ù)化字典:
可以使用字典來參數(shù)化測試。例如:
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ù)化文件:
可以使用文件來參數(shù)化測試。例如:
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)
動態(tài)參數(shù)化:
可以使用 Python 代碼動態(tài)生成參數(shù)。例如:
import pytest import time def get_test_data(): test_data = [] start_time = time.time() while time.time() - start_time < 10: # 運行時間小于 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ù):
可以使用動態(tài)參數(shù)化從外部數(shù)據(jù)源加載測試數(shù)據(jù),例如數(shù)據(jù)庫、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 庫從遠程 API 加載測試數(shù)據(jù),并返回一個測試數(shù)據(jù)列表。然后,使用 @pytest.mark.parametrize
裝飾器動態(tài)參數(shù)化測試,使用從 API 加載的測試數(shù)據(jù)作為參數(shù)。
組合參數(shù):
可以使用 itertools
庫中的 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ù)傳遞給測試函數(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ù)化實例解析的文章就介紹到這了,更多相關(guān)pytest的參數(shù)化實例解析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- pytest實戰(zhàn)技巧之參數(shù)化基本用法和多種方式
- pytest使用@pytest.mark.parametrize()實現(xiàn)參數(shù)化的示例代碼
- pytest?fixtures函數(shù)及測試函數(shù)的參數(shù)化解讀
- pytest使用parametrize將參數(shù)化變量傳遞到fixture
- Python基礎(chǔ)教程之pytest參數(shù)化詳解
- pytest實現(xiàn)測試用例參數(shù)化
- Pytest單元測試框架如何實現(xiàn)參數(shù)化
- Pytest參數(shù)化parametrize使用代碼實例
- pytest參數(shù)化:@pytest.mark.parametrize詳解
相關(guān)文章
python3 常見解密加密算法實例分析【base64、MD5等】
這篇文章主要介紹了python3 常見解密加密算法,結(jié)合實例形式分析了Python的base64模塊加密,以及基于pycrypto模塊的MD5加密等相關(guān)操作技巧,需要的朋友可以參考下2019-12-12python用selenium打開chrome瀏覽器保持登錄方式
大家好,本篇文章主要講的是python用selenium打開chrome瀏覽器保持登錄方式,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02Python HTML解析模塊HTMLParser用法分析【爬蟲工具】
這篇文章主要介紹了Python HTML解析模塊HTMLParser用法,結(jié)合實例形式分析了HTMLParser模塊功能、常用函數(shù)及作為爬蟲工具相關(guān)使用技巧,需要的朋友可以參考下2019-04-04python解析發(fā)往本機的數(shù)據(jù)包示例 (解析數(shù)據(jù)包)
這篇文章主要介紹了使用python解析獲取發(fā)往本機的數(shù)據(jù)包,并打印出來, 大家參考使用吧2014-01-01使用AJAX和Django獲取數(shù)據(jù)的方法實例
這篇文章主要給大家介紹了關(guān)于使用AJAX和Django獲取數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10使用Python爬蟲框架獲取HTML網(wǎng)頁中指定區(qū)域的數(shù)據(jù)
在當今互聯(lián)網(wǎng)時代,數(shù)據(jù)已經(jīng)成為了一種寶貴的資源,無論是進行市場分析、輿情監(jiān)控,還是進行學(xué)術(shù)研究,獲取網(wǎng)頁中的數(shù)據(jù)都是一個非常重要的步驟,Python提供了多種爬蟲框架來幫助我們高效地獲取網(wǎng)頁數(shù)據(jù),本文將詳細介紹如何使用Python爬蟲框架來獲取HTML網(wǎng)頁中指定區(qū)域的數(shù)據(jù)2025-03-03python中將兩組數(shù)據(jù)放在一起按照某一固定順序shuffle的實例
今天小編就為大家分享一篇python中將兩組數(shù)據(jù)放在一起按照某一固定順序shuffle的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07