本篇主要根据实际业务,在本地简单实现了读取测试数据,执行自动化接口测试,写入结果数据。
实际项目中用python脚本实现接口测试的步骤:
1.读取测试数据 , 对数据进行必要的处理-> 2.发送请求,获取token -> 3.请求业务接口,断言响应数据是否与预期一致 -> 4.请求数据&响应结果保存在本地
测试数据样例 (Key-Value)
appId:xxx,appSecret:xxx,productId:xxx,accessId:xxx,idCode:xxx,mobile:xxx,name:xxx
一、读取测试数据,对数据进行必要的处理
首先我们需要先读取文件,去除首尾空格,然后切割中间“,”得到list,并做对于请求异常做处理。
代码语言:javascript复制def read_file(filename):
file = open(filename,"r ",encoding='utf-8')
list_1=[]
try:
for items in file.readlines():
dict_1={}
for item in items.strip("n").split(","): # 去除首尾空格,然后切割中间“,”得到list
dict_1[item.split(":",1)[0]]=item.split(":",1)[1] # 原型:dict[key]=value and list.split["",](得到list)
list_1.append(dict_1)
print("获取参数",list_1)
except ValueError:
print("-----参数参数失败-----")
return list_1
二、发送请求,获取token - Get方式
构建request请求,在这里加入请求头,延迟时间,请求参数。并做对预期结果做检验和请求异常处理。
代码语言:javascript复制def get_token(url, data = None):
header1 = {
"Connection": "keep-alive",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"Host": "10.8.18.171:20220",
}
timeout = random.choice(range(80, 100))
while True:
try:
response = requests.get(url, headers=header1, timeout=timeout, params=data)
jsonstr = json.loads(response.text)
print("获取token响应结果",jsonstr)
code = jsonstr["code"]
message = jsonstr["message"]
token = jsonstr["token"]
assert message == "成功" or code == 1
print('-----pass_1------')
break
except socket.timeout as e:
print(e)
time.sleep(random.choice(range(20, 60)))
except socket.error as e:
print(e)
time.sleep(random.choice(range(0, 60)))
except http.client.BadStatusLine as e:
print(e)
time.sleep(random.choice(range(30, 60)))
except http.client.IncompleteRead as e:
print(e)
time.sleep(random.choice(range(20, 60)))
except ValueError:
print("-----fail-----")
return token
三、请求业务接口 - Post,断言响应数据是否与预期一致
业务接口和上一个接口类似,用Post请求方式传递Json,请求头参数需要关联token以及其他必要参数。并做对预期结果做检验和请求异常处理。
代码语言:javascript复制def post_data(url, jsondata = None,token= None):
header2 = {
"Connection": "close",
"token": token,
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"Content-Type": "application/json;charset=UTF-8",
"X-User-IP": "8.8.8.8",
"Host": "10.8.18.171:20220",
}
timeout = random.choice(range(80, 100))
values_json = json.dumps(jsondata)
print(values_json)
while True:
try:
result = []
response1 = requests.post(url, headers=header2, timeout=timeout, data=values_json)
jsonstr = json.loads(response1.text)
print("查询响应结果",jsonstr)
resultCode = jsonstr["resultCode"]
resultBody = jsonstr["resultBody"]
assert resultBody == "匹配" and resultCode == 1000
print('-----pass_2------')
values_json = json.loads(values_json)
result.append(values_json)
result.append(jsonstr)
break
except socket.timeout as e:
print(e)
time.sleep(random.choice(range(20, 60)))
except socket.error as e:
print(e)
time.sleep(random.choice(range(0, 60)))
except http.client.BadStatusLine as e:
print(e)
time.sleep(random.choice(range(30, 60)))
except http.client.IncompleteRead as e:
print(e)
time.sleep(random.choice(range(20, 60)))
except ValueError:
print("-----fail-----")
return result
四、请求数据&响应结果保存在本地
代码语言:javascript复制def data_output(info, filename):
with open(filename,'a',errors='ignore', newline='') as f:
f_csv = csv.writer(f)
f_csv.writerow(info)
print("-----pass_3------")
print("写入成功")
五、执行
读取文件中的测试数据,for循环依次取出参数,组装测试数据,请求测试接口,保存请求报文和响应报文。
代码语言:javascript复制if __name__ == '__main__':
temp = read_file("testdata.txt")
for i in temp:
payload = {'appId':i['appId'], 'appSecret':i['appSecret']}
token = get_token(token_url,payload)
jsondata = {
"productId":i['productId'],
"accessId":i['accessId'],
"customBody":
{
"idCode": i['idCode'],
"mobile": i['mobile'],
"name": i['name']
}
}
response = post_data(cash_one,jsondata,token)
print(response)
data_output(response,'test.csv')
日志输出示例
代码语言:javascript复制C:Python37python.exe "E:/Python Study/20190902.py"
/*
* 提示:该行代码过长,系统自动注释不进行高亮。一键复制会移除系统注释
* 获取参数 [{'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘背'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '王慧娟'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673944532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673470532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘芳君'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '安帅印'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673950532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '王嘉莹'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘解决'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘他'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '王晋春'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '宋利荣'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '宋腾达'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}, {'appId': '354b54962c5c4ae286a1138e778bff05', 'appSecret': '6c814', 'productId': '30000187', 'accessId': '15673940532091', 'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}]
*/
获取token响应结果 {'code': 1, 'message': '成功', 'token': 'eyJhbGciOiJIUzI1NiJ9.eyJwcm9kdWN0SWRzIjoiMzAwMDAwODgsMzAwMDAwOTcsMzAwMDAwNTcsMzAwMDAxMDEsMzAwMDAxMTgsMzAwMDAxMjcsMzAwMDAxMTUsMzAwMDAxMjksMzAwMDAxMzMsMzAwMDAxMzcsMzAwMDAxNDAsMzAwMDAxMzgsMzAwMDAxMzYsMzAwMDAxMzEsMzAwMDAxNDUsMzAwMDAxNDYsMzAwMDAxNDksMzAwMDAxNDEsMzAwMDAxNTgsMzAwMDAxNjEsMzAwMDAxNjAsMzAwMDAxNjIsMzAwMDAxNzAsMzAwMDAxNjUsMzAwMDAxNzEsMzAwMDAxNzIsMzAwMDAxNzQsMzAwMDAwOTMsMzAwMDAwOTQsMzAwMDAxNzYsMzAwMDAxNzUsMzAwMDAxNzgsMzAwMDAxODIsMzAwMDAxODQsMzAwMDAxODcsMzAwMDAxODksMzAwMDAxOTAsMzAwMDAxOTEsMzAwMDAxOTIsMzAwMDAxOTMiLCJleHAiOjE1Njc0MTY0NTcsInVzZXJJZCI6IjE3NSJ9.oVlkRHUf_SyzxdVWnRUmXvJPVFyeZIzGbtTPddtewHo'}
-----pass_1------
{"productId": "30000187", "accessId": "15673940532091", "customBody": {"idCode": "413027196109271110", "mobile": "16612345678", "name": "u5218u5fb7u534e"}}
查询响应结果 {'resultCode': 1000, 'resultBody': '匹配', 'accessId': '15673940532091', 'transationId': '04ed2274d2077988'}
-----pass_2------
[{'productId': '30000187', 'accessId': '15673940532091', 'customBody': {'idCode': '413027196109271110', 'mobile': '16612345678', 'name': '刘德华'}}, {'resultCode': 1000, 'resultBody': '匹配', 'accessId': '15673940532091', 'transationId': '04ed2274d2077988'}]
-----pass_3------
写入成功
执行保存结果 (请求报文&响应结果)