Pytest框架学习19--参数化2

1、数据源是yaml

安装yaml,使用safe_load方法读取文件,解析出数据

pip install PyYAML

复制代码
# test_data.yaml
test_case_1:
  a: 2
  b: 3
  expected_result: 5

test_case_2:
  a: -1
  b: 10
  expected_result: 9

test_case_3:
  a: 0
  b: 0
  expected_result: 0
python 复制代码
# test_code.py

import yaml
import pytest
from code import add

def load_test_data():
    with open('test_data.yaml', 'r') as file:
        test_data = yaml.safe_load(file)
    return test_data

@pytest.mark.parametrize("input_data", load_test_data().values())
def test_add(input_data):
    a = input_data['a']
    b = input_data['b']
    expected_result = input_data['expected_result']

    result = add(a, b)
    assert result == expected_result, f"计算错误:{a} + {b} 应该得到 {expected_result},实际得到 {result}"

# code.py

def add(a, b):
    return a + b

2、数据源是excel

| Test Case | Operand A | Operand B | Expected Result |

|-----------|------------|------------|------------------|

| Case 1 | 2 | 3 | 6 |

| Case 2 | -1 | 5 | -5 |

| Case 3 | 0 | 10 | 0 |

安装库,然后调用方法读取excel中每一行数据

pip install openpyxl

python 复制代码
# test_code.py

import pytest
from openpyxl import load_workbook
from code import multiply

def load_test_data():
    workbook = load_workbook('test_data.xlsx')
    sheet = workbook.active
    test_data = []

    for row in sheet.iter_rows(min_row=2, values_only=True):
        test_data.append(row)

    return test_data

@pytest.mark.parametrize("test_case, operand_a, operand_b, expected_result", load_test_data())
def test_multiply(test_case, operand_a, operand_b, expected_result):
    result = multiply(operand_a, operand_b)
    assert result == expected_result, f"测试用例 {test_case} 失败:{operand_a} * {operand_b} 应该得到 {expected_result},实际得到 {result}"

# code.py

def multiply(a, b):
    return a * b

3、数据源是csv

operand_a,operand_b,expected_result

2,3,6

-1,5,-5

0,10,0

0.5,2,1

0.5,0.5,0.25

导入csv模块,然后调用DictReader方法,读取csv中每一行数据生成一个列表

java 复制代码
[
  {
    "test_case": "Case 1",
    "operand_a": 2,
    "operand_b": 3,
    "expected_result": 5
  },
  {
    "test_case": "Case 2",
    "operand_a": -1,
    "operand_b": 10,
    "expected_result": 9
  },
  {
    "test_case": "Case 3",
    "operand_a": 0,
    "operand_b": 0,
    "expected_result": 0
  }
]
python 复制代码
# test_code.py

import csv
import pytest
from code import multiply

def load_test_data():
    test_data = []
    with open('test_data.csv', newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            test_data.append(row)
    return test_data

@pytest.mark.parametrize("data", load_test_data())
def test_multiply(data):
    operand_a = int(data['operand_a'])
    operand_b = int(data['operand_b'])
    expected_result = int(data['expected_result'])

    result = multiply(operand_a, operand_b)
    assert result == expected_result, f"{operand_a} * {operand_b} 应该得到 {expected_result},实际得到 {result}"


# code.py

def multiply(a, b):
    return a * b

4、数据源是json

导入json模块,解析数据,然后传参

python 复制代码
# test_code.py

import json
import pytest
from code import add

def load_test_data():
    with open('test_data.json', 'r') as file:
        test_data = json.load(file)
    return test_data

@pytest.mark.parametrize("data", load_test_data())
def test_add(data):
    operand_a = data['operand_a']
    operand_b = data['operand_b']
    expected_result = data['expected_result']

    result = add(operand_a, operand_b)
    assert result == expected_result, f"{operand_a} + {operand_b} 应该得到 {expected_result},实际得到 {result}"

# code.py

def add(a, b):
    return a + b
相关推荐
小叮当⇔几秒前
树莓派4B使用指南
学习·树莓派
straw_hat.2 分钟前
32HAL——万年历
stm32·单片机·学习
snakecy24 分钟前
二叉树、动态规划与链表学习
学习·链表·动态规划
Bathwind-w2 小时前
空间矢量脉宽调制(Space Vector Pulse Width Modulation)SVPWM基础
学习
小苏兮2 小时前
【把Linux“聊”明白】自动化构建-make/Makefile详解
linux·服务器·学习·自动化·1024程序员节
XH1.2 小时前
学习RT-thread(项目一:基于RT-thread的multi_button控制灯闪烁)
stm32·单片机·学习
Wu Liuqi2 小时前
【大模型学习4】大语言模型(LLM)详解
人工智能·学习·语言模型·大模型
CarmenHu3 小时前
RAFT微调学习笔记
笔记·学习
QiZhang | UESTC3 小时前
JAVA算法练习题day67
java·python·学习·算法·leetcode
d111111111d4 小时前
STM32外设学习--ADC模数转换器--笔记
笔记·stm32·单片机·嵌入式硬件·学习