接口自动化测试
一个App项目中有较多的接口,往往在项目更新迭代时不一定能对一些老接口都测试一遍,那么维护一套接口自动化测试用例用来回归测试,还是有一定好处的,既可相对节约一些人力还可以保证接口的正确性。
最近在研究接口自动化测试,写了一个简易的python脚本,方法是脚本调用excel中的接口数据并执行校对。
脚本如下:
#coding:utf-8
import requests #接口
import json #解析返回的json数据
import re #正则表达式
import xlrd #读excel
import xlwt #写excel
import os #对文件的导入导出操作
import socket
from xlutils.copy import copy #复制excel的sheet
excel = 'TestCase.xls' #定义存放用例的excel路径
all_data = xlrd.open_workbook(excel,formatting_info=True)
headers = {'Content-type': 'application/json; charset=utf-8',
'Connection':'keep-alive'} #定义headers
I = [] #用来存放测试结果
J = [] #用例存放实际Status Code
K = [] #用来存放实际Response
table = all_data.sheet_by_index(0)
nrow = table.nrows #获取行数
for i in range(1,nrow): #循环获取每行中的数据
api_host = table.cell(i,2).value #获取excel中API Host
request_url = table.cell(i,3).value #获取excel中Request URL
request_method = table.cell(i,4).value #获取excel中的方法,GET/POST/DELETE
url = api_host + request_url #拼接url
request_data = table.cell(i,5).value.encode("utf-8") #获取excel中要传的参数Request Data
status_code = table.cell(i,6).value #获取期望的状态码Status Code
response = table.cell(i,7).value #获取期望的Response
if request_method == 'GET': #不同的方法发不同的请求
r = requests.get(url,headers = headers)
elif request_method == 'POST':
r = requests.post(url,data = request_data,headers = headers)
elif request_method == 'DELETE':
r = requests.delete(url,data = request_data,headers = headers)
if r.status_code == status_code:
#if json.dumps(r.json(),ensure_ascii=False) == response:
#if r.status_code == status_code and json.dumps(r.json(),ensure_ascii=False) == response:
#print(json.dumps(r.json(),ensure_ascii=False))
I.append('Pass')
J.append('')
K.append(json.dumps(r.json(),ensure_ascii=False))
#K.append('')
else:
#print(json.dumps(r.json(),ensure_ascii=False))
I.append('Fail')
J.append(r.status_code)
try:
K.append(json.dumps(r.json(),ensure_ascii=False))
except:
K.append(re.search("<title>.*</title>", r.text).group().strip("</title>"))
r.close()
print('共有%d个url,第%d个执行完毕'%(nrow-1,i))
book = copy(all_data)
sheet1 = book.get_sheet(0)
for j in range(1,nrow): #将各结果写入到对应的表格中
sheet1.write(j,8,I[j-1])
sheet1.write(j,9,J[j-1])
sheet1.write(j,10,K[j-1])
book.save(excel)
excel如下:
脚本写得比较简陋,后续会进行优化,如果需要接口自动化,目前脚本可以基本不变动,只需对excel进行修改,录入对应接口数据。
脚本中暂时只是对预期状态码进行了校对,并输出测试结果、实际状态码和实际Response等;如果预期Response也能提前录入到excel表的话,当然也可对预期状态码和Response同时进行校对,更加快捷,不用人工去查看返回的Response是否正确。
脚本跑完之后,excel结果如下:
总结:目前能力有限,暂时只能设计到这个程度,后续继续优化脚本和整体框架的设计。