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
相关推荐
北风toto8 分钟前
python学习DataFrame数据结构
数据结构·python·学习
qq_386322691 小时前
华为网路设备学习-26(BGP协议 一)
学习
测试开发技术1 小时前
如何在 Pytest 中调用其他用例返回的接口参数?
面试·自动化·pytest·接口·接口测试·api测试
DKPT2 小时前
Java设计模式之行为型模式(责任链模式)介绍与说明
java·笔记·学习·观察者模式·设计模式
L_autinue_Star2 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
AI360labs_atyun3 小时前
Java在AI时代的演进与应用:一个务实的视角
java·开发语言·人工智能·科技·学习·ai
绿蚁新亭3 小时前
Spring的事务控制——学习历程
数据库·学习·spring
mozun20204 小时前
激光雷达学习-信噪比SNR与信背比SBR2025.7.11
学习·目标检测·信号处理·信噪比·弱小目标检测·信背比
华一精品Adreamer4 小时前
平板柔光屏与镜面屏的区别有哪些?技术原理与适用场景全解析
学习·平板
PNP机器人5 小时前
普林斯顿大学DPPO机器人学习突破:Diffusion Policy Policy Optimization 全新优化扩散策略
人工智能·深度学习·学习·机器人·仿真平台·franka fr3