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. 运行结果
相关推荐
我送炭你添花3 小时前
Pelco KBD300A 模拟器:03.Pelco-P 协议 8 字节完整拆解 + 与 Pelco-D 一一对应终极对照表
python·测试工具·运维开发
R.lin3 小时前
Java 8日期时间API完全指南
java·开发语言·python
西南胶带の池上桜3 小时前
1.Pytorch模型应用(线性与非线性预测)
人工智能·pytorch·python
丘狸尾4 小时前
gradio uv无法add
开发语言·python
全栈陈序员5 小时前
【Python】基础语法入门(十七)——文件操作与数据持久化:安全读写本地数据
开发语言·人工智能·python·学习
爱笑的眼睛115 小时前
FastAPI 路由系统深度探索:超越基础 CRUD 的高级模式与架构实践
java·人工智能·python·ai
越甲八千6 小时前
uvicorn是啥
python
Dxy12393102166 小时前
Python字符串处理全攻略
开发语言·python
Fiona-Dong6 小时前
Louvain 算法
python·算法
坐吃山猪6 小时前
BrowserUse14-源码-ScreenShot模块-整理
linux·数据库·python