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. 运行结果
相关推荐
共享家952733 分钟前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Hgfdsaqwr1 小时前
Python在2024年的主要趋势与发展方向
jvm·数据库·python
一晌小贪欢2 小时前
Python 测试利器:使用 pytest 高效编写和管理单元测试
python·单元测试·pytest·python3·python测试
小文数模2 小时前
2026年美赛数学建模C题完整参考论文(含模型和代码)
python·数学建模·matlab
Halo_tjn2 小时前
基于封装的专项 知识点
java·前端·python·算法
Hgfdsaqwr2 小时前
掌握Python魔法方法(Magic Methods)
jvm·数据库·python
weixin_395448912 小时前
export_onnx.py_0130
pytorch·python·深度学习
s1hiyu2 小时前
使用Scrapy框架构建分布式爬虫
jvm·数据库·python
2301_763472462 小时前
使用Seaborn绘制统计图形:更美更简单
jvm·数据库·python
无垠的广袤3 小时前
【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测
linux·人工智能·python·opencv·开发板