python--读写excle执行测试用例

安装第三方库

pip install  xlrd

pip install xlutils

pip install xlwt

pip install openpyxl  #支持xlsx后缀

excleDir=r'D:/sss.xls'
import xlrd
from xlutils.copy import copy

import json
workBook=xlrd.open_workbook(excleDir,formatting_info=True)#保存样式相同
sheetNames=workBook.sheet_names()#获取各个sheet的表名
# print(sheetNames)
workSheet=workBook.sheet_by_name('登录接口')
# workSheet=workBook.sheet_by_index(1)   #通过下标获取
new_workBook=copy(workBook) #复制出来的新的excle对象
new_workSheet=new_workBook.get_sheet(1)

def login_auto(rows):
    cellData=workSheet.cell_value(rows,6)   #字符串类型
    tc_id=workSheet.cell_value(rows,0)  #字符串类型
    cellExp=json.loads(cellData)
    #传递参数
    res=api_login(cellExp['username'],cellExp['password'])      #登录函数接口

    #判断测试用例是否通过
    res_exp=json.loads(workSheet.cell_value(rows,8))['retcode'] #获取预期结果
    if res['retcode']==res_exp:
        print(f'{tc_id}---pass---')
        info='pass'
    else:
        print(f'{tc_id}---false---')
        info='false'

    new_workSheet.write(rows,9,info)    #行,列,内容
for cnt in range(1,5):
    login_auto(cnt)
new_workBook.save(r'./res.xls')

猜你喜欢

转载自www.cnblogs.com/guang2508/p/13398375.html
今日推荐