pytest(二)excel数据驱动

一、excel数据驱动

  1. excel文件内容
  2. excel数据驱动使用方法
python 复制代码
import openpyxl
import pytest

def get_excel():
    excel_obj = openpyxl.load_workbook("../pytest结合数据驱动-excel/data.xlsx")
    sheet_obj = excel_obj["Sheet1"]
    values = sheet_obj.values
    case_list = []

    for row in values:
        data = []
        if row[0] != 'A':
            for cell in row:
                data.append(cell)
            case_list.append(data)
    print(case_list)        #[[1, 1, 2], [3, 6, 9], [100, 200, 300]]
    return case_list


def my_add(x, y):
    # print(x,y)
    result = x + y
    return result

class TestWithExcel:
    @pytest.mark.parametrize('x,y,expected',get_excel())
    def test_add(self, x, y, expected):
        # print(x,y,expected)
        print(f"{x} + {y} = {expected}")
        assert my_add(int(x), int(y))== int(expected)


if __name__ == '__main__':
    pytest.main(["-s", "test_caseexcel.py"])
  1. 运行结果
相关推荐
吴佳浩20 小时前
LangChain 深入
人工智能·python·langchain
网安-轩逸1 天前
回归测试原则:确保软件质量的基石
自动化测试·软件测试·python
Mr_Xuhhh1 天前
YAML相关
开发语言·python
咖啡の猫1 天前
Python中的变量与数据类型
开发语言·python
汤姆yu1 天前
基于springboot的电子政务服务管理系统
开发语言·python
执笔论英雄1 天前
【RL】python协程
java·网络·人工智能·python·设计模式
帮帮志1 天前
【AI大模型对话】流式输出和非流式输出的定义和区别
开发语言·人工智能·python·大模型·anaconda
jquerybootstrap1 天前
大地2000转经纬度坐标
linux·开发语言·python
Y***89081 天前
【JAVA进阶篇教学】第十二篇:Java中ReentrantReadWriteLock锁讲解
java·数据库·python
DanB241 天前
Java(多线程)
java·开发语言·python