前段时间在 testhome 的 QQ 群中看到了 ‘BJ-行者’ 说的接口自动化测试,一时兴起,想着自己也写个,说干就干吧,具体思路如下:
接下来还是上代码说明测试思路:
for i in range(1,nrows):
n = 0
address = sheet.cell(i,1).value
name = sheet.cell(i,2).value
# 获取第i+1行,第2列的数据
method =sheet.cell(i,3).value
print 'address:'+address+' method:'+method
data =sheet.cell(i,5).value
for j in range(1,sub_nrows):
sub_testId = sub_sheet.cell(j,0).value
testId = 'Sheet2' + ':' + sub_testId
sub_ncol = sub_sheet.row_values(j)
# 获取第j+1行所有的数据
sub_ncols = len(sub_ncol)
# 计算当前行的列数
for a in range(1,sub_ncols):
if sub_ncol[a] == '':
sub_ncols = sub_ncols -1
# for循环,排除为空值的列
if (data == testId):
n = n+1
m = j-n
# 为了保证参数名称每次都一样,所有当j值加1后,然后通过n传参再减1,这样就保证当前用例的m值始终一致
listA=[]
# 初始化一A数组
for k in range(1,sub_ncols):
parameter1 = sub_sheet.cell(j,k).value
# k为列值变量,从第4列开始计数,依次读取某一行的所有列的数据
parameter2 = sub_sheet.cell(m,k).value
if type(parameter1).__name__ == "unicode":
Parameter1 = parameter1.encode("utf-8")
# 字符类型转换,若当前为unicode类型,则转换为utf-8类型
if type(parameter1).__name__=="float":
Parameter1 = str(parameter1)
# print Parameter1
if type(parameter2).__name__ == "unicode":
Parameter2 = parameter2.encode("utf-8")
# print Parameter2
parameter = "'" + Parameter2 + "'" + ":" + "'" + Parameter1 + "'" + ","
#print Parameter
listA.append(parameter)
# 将参数parameter添加到数组A中
listB = '{' + ''.join(listA) + '}'
listC = eval(listB)
# 将数组A中所有的数据进行拼接,然后通过eval转成unicode类型
params = urllib.urlencode(Parameter)
headers = {"Content-type": "application/x-www-form-urlencoded"}
httpClient = httplib.HTTPConnection("test.qshealthcare.com", 80, timeout=30)
httpClient.request(Method, Address, params, headers)
resp = httpClient.getresponse()
print resp.status
print resp.reason
result = resp.read()
print result
result = result.encode("utf-8")
my_data = json.loads(result)
if(my_data['message'] == u"success"):
# 用于判断my_data输出中message的值是否为success,以此判断用例是否执行成功
flag = True
else:
flag = False
Flag.append(flag)
t = get_template('report.html') #在settings.py中需要设置模板的路径
excel_data(xlsfile1)
html = t.render(Context({'datas':datas,'Case_time':time,'Case_Tnum':Tnum,'Case_Fnum':Fnum,'Case_num':num}))
return HttpResponse(html)