1、数据源是yaml
安装yaml,使用safe_load方法读取文件,解析出数据
pip install PyYAML
# test_data.yaml
test_case_1:a: 2b: 3expected_result: 5test_case_2:a: -1b: 10expected_result: 9test_case_3:a: 0b: 0expected_result: 0
# test_code.pyimport yaml
import pytest
from code import adddef 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.pydef 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
# test_code.pyimport pytest
from openpyxl import load_workbook
from code import multiplydef load_test_data():workbook = load_workbook('test_data.xlsx')sheet = workbook.activetest_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.pydef 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中每一行数据生成一个列表
[{"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}
]
# test_code.pyimport csv
import pytest
from code import multiplydef 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.pydef multiply(a, b):return a * b
4、数据源是json
导入json模块,解析数据,然后传参
# test_code.pyimport json
import pytest
from code import adddef 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.pydef add(a, b):return a + b