欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2023年07月12日 09:47:31   作者:菜鳥小超  
這篇文章主要介紹了Python中pytest的參數(shù)化實例解析,pytest是一個非常成熟的全功能的Python測試框架,主要有簡單靈活,容易上手,支持參數(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論