python 读取excel数据(Dtt+Pytest)




 1 import xlrd
 2 import pytest
 3 from testcase.basic import get_project_path
 4 
 5 
 6 def get_excel_data(filename, sheet_name):
 7     project_path = get_project_path.get_project_path()  # 调用函数,获取当前项目根目录
 8     file_path = project_path + '/data/' + filename  # 读取的excel文件路径
 9     lst_data = []
10     work_book = xlrd.open_workbook(file_path)
11     work_sheet = work_book.sheet_by_name(sheet_name)
12     rows = work_sheet.nrows
13     cols = work_sheet.ncols
14     for row in range(1, rows):  # 遍历时不取第一行表头内容
15         row_list = []
16         for col in range(cols):
17             cell_data = work_sheet.cell_value(row, col)
18             row_list.append(cell_data)  # 每一行的内容都放到一个列表中
19         lst_data.append(row_list)  # 把多行内容放到同一个列表中
20     return lst_data
21 
22 
23 filename = 'login.xls'
24 data = get_excel_data(filename, "login")
25 
26 
27 @pytest.mark.parametrize('name,age,six', data)
28 def test_excel(name, age, six):
29     print(name, age, six)
30 
31 
32 if __name__ == '__main__':
33     # print(get_excel_data())
34     pytest.main(['-vs', 'test_excel.py'])
附上:'login.xls  截图: