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. 运行结果
相关推荐
翔云12345612 分钟前
(MySQLdb._exceptions.OperationalError) (2006, ‘MySQL server has gone away‘)
网络·python
我是一只小青蛙88827 分钟前
Python办公自动化:6大实用库速览
python
Duang007_38 分钟前
【LeetCodeHot100 超详细Agent启发版本】两数之和 (Two Sum)
java·人工智能·python
企业对冲系统官1 小时前
基差风险管理系统集成说明与接口规范
大数据·运维·python·算法·区块链·github
花酒锄作田1 小时前
[python]Flask - Tracking ID的设计
python·flask·pytest
PeterClerk1 小时前
计算机视觉常用指标(Metrics)速查与解释(持续更新)
人工智能·python·深度学习·计算机视觉·benchmark·评测
哪有时间简史2 小时前
Python程序设计基础
开发语言·python
企业对冲系统官2 小时前
大宗商品风险对冲系统统计分析功能的技术实现
运维·python·算法·区块链·github·pygame
ValhallaCoder2 小时前
Day48-单调栈
数据结构·python·算法·单调栈
智算菩萨2 小时前
【Python小游戏】深度解析Pygame实现2048游戏的完整开发流程(有代码实现)
python·游戏程序·pygame