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. 运行结果
相关推荐
List<String> error_P7 分钟前
Python蓝桥杯常考知识点-模拟
开发语言·python·蓝桥杯
比奇堡鱼贩23 分钟前
python第五次作业
开发语言·前端·python
码农小韩1 小时前
AIAgent应用开发——DeepSeek分析(二)
人工智能·python·深度学习·agent·强化学习·deepseek
喵手2 小时前
Python爬虫实战:构建一个高健壮性的图书数据采集器!
爬虫·python·爬虫实战·零基础python爬虫教学·构建图书数据·采集图书数据·图书数据采集
张3蜂3 小时前
Python venv 详解:为什么要用、怎么用、怎么用好
开发语言·python
老赵全栈实战3 小时前
《从零搭建RAG系统第3天:文档加载+文本向量化+向量存入Milvus》
python
火龙果研究院3 小时前
在CentOS上安装Python 3.13需要从源码编译
开发语言·python·centos
龙山云仓3 小时前
No156:AI中国故事-对话司马迁——史家绝唱与AI记忆:时间叙事与因果之链
大数据·开发语言·人工智能·python·机器学习
niuniudengdeng3 小时前
一种基于高维物理张量与XRF实景复刻的一步闭式解工业级3D打印品生成模型
人工智能·python·数学·算法·3d
overmind4 小时前
oeasy Python 114 在列表指定位置插入insert
开发语言·python