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. 运行结果
相关推荐
m0_624578591 小时前
MySQL主从复制支持跨版本吗_不同版本间同步的注意事项
jvm·数据库·python
yuanpan1 小时前
Python Pygame 入门教程:从零学会创建窗口、绘图和游戏交互
python·游戏·pygame
2401_871492851 小时前
如何在 React Router v6 中正确配置多路由组件显示
jvm·数据库·python
神仙别闹2 小时前
基于Python(Django)+MySQL 实现(Web)SQL智能检测系统的设计与实现
python·mysql·django
甄心爱学习2 小时前
【项目实训】法律文书智能摘要系统4
python·github·个人开发
huzhongqiang3 小时前
Playwright理解与封装
python
zhangchaoxies3 小时前
MySQL触发器能否监控特定用户操作_结合审计功能实现分析
jvm·数据库·python
qq_413502023 小时前
如何解决ORA-12518监听程序无法分配进程_内存耗尽与PGA溢出
jvm·数据库·python
zhangrelay3 小时前
三分钟云课实践速通--大学物理--python 版
linux·开发语言·python·学习·ubuntu·lubuntu
djjdjdjdjjdj4 小时前
如何用参数解构在函数入口处直接提取对象属性
jvm·数据库·python