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
相关推荐
YJlio1 小时前
Windows Sysinternals 文件工具学习笔记(12.11):综合实战——从磁盘告警到文件替换的一条龙排障
windows·笔记·学习
旖旎夜光3 小时前
Linux(4)(下)
linux·学习
敲敲了个代码6 小时前
从硬编码到 Schema 推断:前端表单开发的工程化转型
前端·javascript·vue.js·学习·面试·职场和发展·前端框架
我命由我123458 小时前
SVG - SVG 引入(SVG 概述、SVG 基本使用、SVG 使用 CSS、SVG 使用 JavaScript、SVG 实例实操)
开发语言·前端·javascript·css·学习·ecmascript·学习方法
Fern_blog10 小时前
鸿蒙学习之路
学习
小智RE0-走在路上11 小时前
Python学习笔记(11) --数据可视化
笔记·python·学习
少云清13 小时前
【接口测试】4_代码实现 _pytest框架
pytest·接口测试
Asus.Blogs13 小时前
SSE + Resty + Goroutine + Channel 完整学习笔记
笔记·学习·golang
屋顶那猫13 小时前
使用pyinstaller打包pytest项目
python·pytest