最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中pytest的參數(shù)化實(shí)例解析

 更新時(shí)間:2023年07月12日 09:47:31   作者:菜鳥小超  
這篇文章主要介紹了Python中pytest的參數(shù)化實(shí)例解析,pytest是一個(gè)非常成熟的全功能的Python測(cè)試框架,主要有簡(jiǎn)單靈活,容易上手,支持參數(shù)化等特點(diǎn),需要的朋友可以參考下

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

宁海县| 石首市| 永善县| 永仁县| 三明市| 昆明市| 馆陶县| 灌云县| 永修县| 大新县| 简阳市| 自贡市| 蒲江县| 湟源县| 兴仁县| 盐亭县| 曲阜市| 新巴尔虎左旗| 富民县| 石楼县| 嘉义县| 南平市| 沂水县| 正镶白旗| 星子县| 闸北区| 克拉玛依市| 额济纳旗| 鄂温| 陆川县| 东山县| 旬邑县| 当涂县| 昂仁县| 乐昌市| 澳门| 阿鲁科尔沁旗| 瓮安县| 涡阳县| 金堂县| 香港|