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. 运行结果
相关推荐
喵手4 分钟前
Python爬虫零基础入门【第九章:实战项目教学·第17节】内容指纹去重:URL 变体/重复正文的识别!
爬虫·python·爬虫实战·python爬虫工程化实战·零基础python爬虫教学·内容指纹去重·url变体
喵手27 分钟前
Python爬虫零基础入门【第五章:数据保存与入库·第1节】先学最通用:CSV/JSONL 保存(可复现、可分享)!
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·数据保存与入库·csv/jsonl
子夜江寒36 分钟前
OpenCV 学习:图像拼接与答题卡识别的实现
python·opencv·学习·计算机视觉
bjxiaxueliang41 分钟前
一文掌握Python Flask:HTTP微服务开发从入门到部署
python·http·flask
SunnyRivers1 小时前
Python 中的 HTTP 客户端:Requests、HTTPX 与 AIOHTTP 对比
python·httpx·requests·aiohttp·区别
u0109272712 小时前
持续集成/持续部署(CI/CD) for Python
jvm·数据库·python
lixin5565562 小时前
基于迁移学习的图像风格增强器
java·人工智能·pytorch·python·深度学习·语言模型
阡陌..2 小时前
浅谈SAR图像处理---形态学滤波
图像处理·人工智能·python
qq_229058013 小时前
python-Dgango项目收集静态文件、构建前端、安装依赖
开发语言·python
测试人社区—66793 小时前
2025区块链分层防御指南:AI驱动的安全测试实战策略
开发语言·驱动开发·python·appium·pytest