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
相关推荐
青春男大1 小时前
java栈--数据结构
java·开发语言·数据结构·学习·eclipse
mashagua2 小时前
RPA系列-uipath 学习笔记3
笔记·学习·rpa
沐泽Mu2 小时前
嵌入式学习-QT-Day05
开发语言·c++·qt·学习
锦亦之22333 小时前
cesium入门学习二
学习·html
m0_748256143 小时前
前端 MYTED单篇TED词汇学习功能优化
前端·学习
IT古董4 小时前
【机器学习】机器学习的基本分类-半监督学习(Semi-supervised Learning)
学习·机器学习·分类·半监督学习
jbjhzstsl4 小时前
lv_ffmpeg学习及播放rtsp
学习·ffmpeg
青い月の魔女4 小时前
数据结构初阶---二叉树
c语言·数据结构·笔记·学习·算法
网络安全(king)4 小时前
网络安全攻防学习平台 - 基础关
网络·学习·web安全
虾球xz5 小时前
游戏引擎学习第59天
学习·游戏引擎