不用框架,原生使用python做注册接口/登陆接口/充值接口的测试,做的数据/代码分离
注意充值时候,cookie的处理方法,还是原来的三种方法
1.操作数据文件 do_exclel.py
1 # -*- conding:utr-8 -*- 2 #@Time :2018/11/8 22:46 3 #@Author:GYP测试 4 #@File :do-excel.py 5 from openpyxl import load_workbook 6 7 class Read_data: 8 def __init__(self,file_name,sheet_name): 9 self.file_name=file_name 10 self.sheet_name=sheet_name 11 #读取头消息 12 def get_header(self): 13 wb=load_workbook(self.file_name) 14 sheet=wb[self.sheet_name] 15 header=[] 16 for i in range(1,sheet.max_column+1): 17 header.append(sheet.cell(1,i).value) 18 return header 19 #读取Excel中的测试数据 20 def read_data(self): 21 wb = load_workbook(self.file_name) 22 sheet = wb[self.sheet_name] 23 header=self.get_header() 24 read_test_data=[] 25 for i in range(2,sheet.max_row+1): 26 sub_data={} 27 for j in range(1,sheet.max_column): 28 sub_data[header[j-1]]=sheet.cell(i,j).value 29 read_test_data.append(sub_data) 30 return read_test_data 31 #测试结果写入Excel中 32 class write_data: 33 def __init__(self,file_name,sheet_name,value): 34 self.file_name=file_name 35 self.sheet_name=sheet_name 36 self.value=value 37 def write_data(self): 38 wb = load_workbook(self.file_name) 39 sheet = wb[self.sheet_name] 40 # for i in range(len(self.value)): 41 for rownum in range(2,sheet.max_row+1): 42 # print(rownum) 43 sheet.cell(rownum,sheet.max_column).value = self.value[rownum-2] 44 wb.save(self.file_name) 45 if __name__ == '__main__': 46 # res1 = Read_data('E:\\test\API_AUTO\\test_data\data.xlsx','login').read_data() 47 # print(res1) 48 data=['a','b','c',1,2] 49 res2= write_data('D:\\autou_testing\API_AUTO\\test_data\data.xlsx','login',data).write_data()
2.http请求的写法
1 # -*- conding:utr-8 -*- 2 #@Time :2018/11/8 21:56 3 #@Author:GYP测试 4 #@File :requests_testing.py 5 import requests 6 class requests_test: 7 def test_api(self,http_method,url,data,cookie=None): 8 if http_method=='post': 9 res=requests.post(url,data,cookies=cookie) 10 else: 11 res=requests.get(url,data,cookies=cookie) 12 return res 13 if __name__ == '__main__': 14 url_login = 'http://119.23.241.154:8080/futureloan/mvc/api/member/login' 15 url_recharge = 'http://119.23.241.154:8080/futureloan/mvc/api/member/recharge' 16 login_data = {"mobilephone": "18688773467", "pwd": "123456"} 17 recharge_data = {"mobilephone": "18688773467", "amount": "1000"} 18 # 登录 19 res = requests_test().test_api('post',url_login,login_data) 20 print("登录结果是:", res.json()) 21 # 充值 22 recharge_res = requests_test().test_api('get', url_recharge, recharge_data, res.cookies) 23 print("充值结果是:", recharge_res.json())
3.通过run文件,运行,后在excel中查看测试结果
run.py
1 # -*- conding:utf-8 -*- 2 #@Time :2018/11/9 10:07 3 #@Author:GYP测试 4 #@File :run.py 5 6 7 from tools.do_excel import write_data,Read_data 8 from tools.http_requests1 import requests_test 9 10 cookie=None 11 def run(sheet_name): 12 global cookie 13 res1=Read_data('D:\\autou_testing\API_AUTO\\test_data\data.xlsx',sheet_name).read_data() 14 test_data=[] 15 for item in res1: 16 res = requests_test().test_api(item['http_menod'],item['url'],eval(item['data']),cookie) 17 if res.cookies: 18 cookie=res.cookies 19 test_data.append(res.json()['code']) 20 #写入预期结果 21 write_data('D:\\autou_testing\API_AUTO\\test_data\data.xlsx',sheet_name,test_data).write_data() 22 #注册接口 23 run('register') 24 #登陆接口 25 run('login') 26 #充值接口 27 run('recharge')
今天新内容:
1.requests.ssesion,,会话中的用法
a.登陆请求后,其他接口不用带cookie的
b.get和post请求,只有一个参数,其他的关键字参数,请看结合视频查看源码
2.大数据处理的模块pandas使用的介绍,详细情况下面链接博客
https://www.cnblogs.com/liulinghua90/p/9935642.html