python request接口测试笔记(1)

python request接口测试笔记(1)

涉及到的功能说明:

需要登录拿到token,才能进行下一个接口的请求

读取csv文件中的信息,作为接口的参数

将接口响应结果,写入csv文件,以便分析统计

# -*- coding:utf-8 -*- ''' author:***cy time:2019-08-28 主要功能说明: 1、获取登录接口的token 2、接入食物识别的接口,批量测试食物识别模型,得出模型的正确率 ''' import requests import json import csv class FoodDetect(): def __init__(self): print("----------------start----------------") '''获取token''' def login_token(self): # 初始化登录接口 login_url = 'https://api.ty.com/oauth2/token?grant_type=password&sms_verify=true' login_header = {"Authorization": "Basic T23DZZMfrrdFrTfdfR4esdFGrfrdfOjdmTFJyEmFmYzgxRDgwSfdfR="} login_data = { "username": "15012345678", "password": "123456", "appName": "ty", "grant_type": "password", "sms_verify": "true" } # 登录请求接口 r_login = requests.post(url=login_url, data=login_data, headers=login_header) # 获取登录的响应报文 print(r_login.text) # login_response = json.loads(r_login.text) # 保存登录的token信息 # access_token = login_response['access_token'] token = r_login.json()['access_token'] print(token) '''请求接口获取token值''' return token def detect_food(self): # 初始化 food_flag_init = 0 food_error_init = 0 food_fail_init = 0 # 读取csv文件 with open('E:\\test\\ty\\food_detect_init.csv', 'r') as csvFile: reader = csv.reader(csvFile) print(type(reader)) next(reader) for row in reader: # 食物识别问答接口的请求参数msg food_msg = {"msg": row[0]} print(food_msg) # 获取登录接口中的token food_headers = {"Authorization": "Bearer " + self.login_token()} # 食物识别接口url food_url = 'https://api.ty.com/nlp/api/v1.0/food_detect' # 食物识别模型接口请求 r_food = requests.post(url=food_url, data=food_msg, headers=food_headers) # 获取响应报文 print(r_food.text) # 转换响应结果为dict格式 food_response = json.loads(r_food.text) # 判断响应结果是否为空,不为空,则获取cal_name和name的名称 if food_response: print(food_response[0]) food_response_all = food_response[0] food_response_all_one = food_response_all['properties'] # 获取cal_name的值 food_response_cal = food_response_all_one['cal_name'] food_response_name = food_response_all_one['name'] print("food的calname:" + food_response_cal) #food_response_name if food_response_cal == row[0]: # 如果calname和输入的食物名称一致,则测试通过,写入food_py_success文件 """ 模型匹配的方法:先匹配name,然后和库里面的cal_name对比,这里需要修改一下判断条件 """ food_flag_init = food_flag_init + 1 print("food匹配成功%d" %food_flag_init) else: fail_data = ['food_name','name','cal_name'] food_fail_init = food_fail_init + 1 print("food匹配错误%d" %food_fail_init) with open('E:\\test\\ty\\food_detect_fail.csv','a+',encoding='utf-8-sig') as ff: fail_data[0] = row[0] fail_data[1] = food_response_name fail_data[2] = food_response_cal ff.write(','.join(fail_data)) ff.write('\n') ff.close() else: food_error_init = food_error_init + 1 print("food匹配失败%d" % food_error_init) # 将失败的食物name存在csv文件 error_data = ['name'] with open('E:\\test\\ty\\food_detect_error.csv','a+',encoding = 'utf-8-sig')as ef: error_data[0] = row[0] ef.write(','.join(error_data)) ef.write('\n') ef.close() # 写入csv文件 data_row = ['food_real_name', 'food_response_name','food_response_cal'] with open('E:\\test\\ty\\food_detect_py.csv','a+',encoding='utf-8-sig') as f: # csv_write = csv.writer(f) data_row[0] = row[0] data_row[1] = food_response_name data_row[2] = food_response_cal # data_row = list(data_row) # print(data_row) f.write(','.join(data_row)) f.write("\n") # f.write("\\n") print("down") f.close() # 打印食物匹配结果 print("food匹配成功%d" % food_flag_init) print("food匹配错误%d" % food_fail_init) print("food匹配失败%d" % food_error_init) csvFile.close() if __name__ == '__main__': fd = FoodDetect() fd.detect_food()

食物检测接口的响应结果格式如下:

[ { "properties":{ "cal_name":"苹果汁", "type":"food", "index":4, "food_id":4342, "kcal_unit_weight":53.56, "name":"苹果汁", "start_index":0, "time_stamp":1567226386 }, "sub_properties":{ "grams":543, "is_default":true, "quantifier_id":54, "quantity":1, "unit":"瓶" }, "type":"Entity" } ]

所以这里想要获取响应结果中的cal_name,需要先获取properties,代码如下:

food_response_all = food_response[0] food_response_all_one = food_response_all['properties'] # 获取cal_name的值 food_response_cal = food_response_all_one['cal_name']

整体下来,批量测试接口模型的工作就是这样,其中将接口返回结果写入csv时,需要注意要换行写入,而且使用写入的格式是'a'或'a+'追加模式。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpjfjy.html