深圳专业高端网站建设费用,网络推广入门教程,事业单位考试网站,做简约头像网站python 读取 Excel 自动化执行测试用例
Excel 测试用例的读取
安装 python 操作Excel 的库
pip install xlrd/xlwt/xlutils (安装三个操作库)
接口请求代码构建
import requests
from random import randint
import xlrd
import json
class My_request(object):
# 对返回值进行…python 读取 Excel 自动化执行测试用例
Excel 测试用例的读取
安装 python 操作Excel 的库
pip install xlrd/xlwt/xlutils (安装三个操作库)
接口请求代码构建
import requests
from random import randint
import xlrd
import json
class My_request(object):
# 对返回值进行处理
def __str__(self):
# return %r%self.connect()
return str(%r%self.connect())
# 获取表格数据
def connect(self):
excelDir ../data/动画绘本馆测试用例.xls
# 打开Excel文件 formatting_infoTrue :保持原样式
self.workBook xlrd.open_workbook(excelDir,formatting_infoTrue)
# 获取所有表名
sheets self.workBook.sheet_names()
# 需要执行的sheet
workSheet self.workBook.sheet_by_name(测试用例2)
# 获取一行
# cellData workSheet.row_values(1)
# 获取单元格 获取的不是字符串 而是excel 格式参数
cellData workSheet.cell(1, 0)
return cellData.value
# token函数 调用函数就可获取token值 再用到新增接口
def get_token(self):
# 获取token
token_urls http://47.96.181.17:9090/rest/toController
payload {userName: J201903070064, password: 362387359}
# 发送请求 json 请求体
res requests.post(token_urls, jsonpayload)
return res.json()[token]
# 新增用户接口
def add_user(self,bodyData)::param bodyData: request body
:return: res dataself.addUsr_url http://47.96.181.17:9090/rest/ac01CrmController
# 对字符串转换字典 json 串
self.payload json.loads(bodyData)
# 对手机号进行参数化
self.payload[aac030] f134{randint(11111111,99999999)}
self.header_addusr {Content-Type: application/json, X-AUTH-TOKEN: self.get_token()}
# # 发送请求 url 请求体
res requests.post(self.addUsr_url,jsonself.payload,headers self.header_addusr)
self.rest res.json()
# 打印输出结果 是否注册成功
print(res.json())
return res.json()
def write_index(self,datas):
from xlutils.copy import copy
# 对表复制 原始的测试用例不会改变
newWoekBook copy(self.workBook)
# 取拷贝的excel的sheet 下标
newSheet newWoekBook.get_sheet(1)
# 写入数据 第一行第一列
newSheet.write(1, 1, datas)
# 保存新生成的 excel对象
newWoekBook.save(../data/res.xls)
def write_in(self):
# 如果已经有数据了 就需要对原表进行复制
if self.rest[message] 成功:
info pass
self.write_index(info)
else:
info fail
self.write_index(info)
if __name__ __main__:
re My_request()
re.add_user(re.connect())
re.write_in()
测试结果写入到Excel
文件不存在 — 新建excel — 写 — xlwt
文件本身存在–另存为写入excel–xlutils
def write_index(self,datas):
from xlutils.copy import copy
# 对表复制 原始的测试用例不会改变
newWoekBook copy(self.workBook)
# 取拷贝的excel的sheet 下标
newSheet newWoekBook.get_sheet(1)
# 写入数据 第一行第一列
newSheet.write(1, 1, datas)
# 保存新生成的 excel对象
newWoekBook.save(../data/res.xls)
def write_in(self):
# 如果已经有数据了 就需要对原表进行复制
if self.rest[message] 成功:
info pass
self.write_index(info)
else:
info fail
self.write_index(info)