pytest结合xlrd实现excel数据驱动

一、Selenium怎么读取excel文件

1、安装xlrd模块
2、使用xlrd模块来处理excel文件
3、结合pytest的参数化处理方式来实现ddt

二、示例
新建一个excel文件:test_excel.xlsx
在这里插入图片描述
新建一个test_excel.py文件

# -*- coding: utf-8 -*-
# @Time: 2022/5/29 4:58 下午
# @Author: wcystart
# @File: test_excel.py
# @description:
import pytest
import xlrd


def get_data():
    filename = "test_excel.xlsx"
    wb = xlrd.open_workbook(filename)
    sheet = wb.sheet_by_index(0) #索引为0的位置对应excel中的第一行
    rows = sheet.nrows 
    clos = sheet.ncols
    my_data = []
    for row in range(rows):
        for col in range(clos):
            cell_data = sheet.cell_value(row, col)
            my_data.append(cell_data)
    return my_data


@pytest.mark.parametrize('name', get_data())
def test01(name):
    print(name)


if __name__ == '__main__':
    pytest.main(['-vs', 'test_excel.py'])

输出结果:
test_excel.py::test01[tom] tom
PASSED
test_excel.py::test01[rose] rose
PASSED
test_excel.py::test01[tina] tina
PASSED
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值