首页 > 其他 > 详细

接口自动化第一阶段代码

时间:2019-02-02 14:31:39      阅读:160      评论:0      收藏:0      [点我收藏+]

 目标:

一、实现单一接口的请求http_request.py

request模块完成,http请求,生成类

加断言,处理异常,用try except 

二、完成读取数据,并且批量写回到excl----new_do_excl.py完成

技术分享图片

# new_do_excl代码
from openpyxl import load_workbook


class DoExcl:

def get_data(self, file_name, sheet_name):
file = load_workbook(file_name)
sheet = file[sheet_name]

test_data = []
new_tel = file["tel"].cell(1, 1).value

for i in range(2, sheet.max_row+1): # +1取左不取右,max_clumn

sub_data = {}
sub_data["id"] = sheet.cell(i, 1).value
sub_data["HttpMethod"] = sheet.cell(i, 2).value
sub_data["module"] = sheet.cell(i, 3).value
sub_data["description"] = sheet.cell(i, 4).value
sub_data["url"] = sheet.cell(i, 5).value
# if sheet.cell(1, 6).value.find("{$tel}") != -1:
# sub_data[‘param‘] = sheet.cell(i, 6).value.replace(‘${tel}‘, str(new_tel))
# else:
sub_data["param"] = sheet.cell(i, 6).value

sub_data["ExpectedResult"] = sheet.cell(i, 7).value
test_data.append(sub_data)
file["tel"].cell(1, 1).value = new_tel+1
file.save(file_name)
return test_data

def write_back(self, file_name, sheet_name, row, ActualResult, TestResult):
wb = load_workbook(file_name)
sheet = wb[sheet_name]
sheet.cell(row, 8).value = ActualResult
sheet.cell(row, 9).value = TestResult
wb.save(file_name)


if __name__ == ‘__main__‘:
test_data = DoExcl().get_data(‘api.xlsx‘,‘testcase‘)
print("测试数据是:{}".format(test_data))

#http_request代码
import requests


class HttpMethod:

def http_request(self,url,param,http_method):
if http_method.upper() ==‘POST‘:
try:
res = requests.post(url, param)
except Exception as e:
print("post注册请求出错,错误是{}".format(e))
else:
try:
res = requests.get(url, param)
except Exception as e:
print("post注册请求出错,错误是{}".format(e))

print("http请求的结果是{}".format(res.json()))
return res.json() # 增加返回值,在请求正常的情况下有返回值


# if __name__ == ‘__main__‘:
# url = ‘http://47.107.168.87:8080/futureloan/mvc/api/member/register‘
# param = {"mobilephone": "18801002528", "pwd": "123456", "regname": "lemonban011"}
# HttpMethod().http_request(url,param,"post")

# run代码
import requests


class HttpMethod:

def http_request(self,url,param,http_method):
if http_method.upper() ==‘POST‘:
try:
res = requests.post(url, param)
except Exception as e:
print("post注册请求出错,错误是{}".format(e))
else:
try:
res = requests.get(url, param)
except Exception as e:
print("post注册请求出错,错误是{}".format(e))

print("http请求的结果是{}".format(res.json()))
return res.json() # 增加返回值,在请求正常的情况下有返回值


# if __name__ == ‘__main__‘:
# url = ‘http://47.107.168.87:8080/futureloan/mvc/api/member/register‘
# param = {"mobilephone": "18801002528", "pwd": "123456", "regname": "lemonban011"}
# HttpMethod().http_request(url,param,"post")


























 

接口自动化第一阶段代码

原文:https://www.cnblogs.com/lin-yue/p/10294744.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!