数据驱动测试DDT之Selenium读取Excel文件
这篇文章主要为大家介绍了数据驱动测试DDT之Selenium读取Excel文件,
(1)安装xlrd pip3 install xlrd
(2)示例脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import pytest import xlrd def get_data(): filename = "F:\\学习\\自动化测试\\selenium自动化测试\\selenium_test\\data\\test.xls" # 读取工作簿 wb = xlrd.open_workbook(filename) # 读取第一个sheet页 sheet = wb.sheet_by_index( 0 ) # 读取行 rows = sheet.nrows # 读取列 cols = sheet.ncols lst = [] for row in range (rows): for col in range (cols): #根据行列获得单元格数据 cell_data = sheet.cell_value(row,col) lst.append(cell_data) return lst @pytest .mark.parametrize( 'name' ,get_data()) def test1(name): print (name) if __name__ = = '__main__' : pytest.main([ '-sv' , 'test.xls' ])
|
运行结果:
【常见问题】:运行测试脚本报错误。
最终发现原因是最近xlrd更新到了2.0.1版本,只支持.xls文件。
【解决方法】:
(1)脚本中使用xls文件
(2)可以安装旧版xlrd,在cmd中运行:
pip3 uninstall xlrd
pip3 install xlrd==1.2.0
以上来自极客时间课程:selenium自动化测试学习总结
以上就是数据驱动测试DDT之Selenium读取Excel文件的详细内容
原文链接:https://blog.csdn.net/aovenus/article/details/121040054