测试HTTP接口的python自动化脚本

来源:互联网 发布:mac序列号不可用 编辑:程序博客网 时间:2024/05/25 21:35

为了用要用python把测试结果写进excel,费了点脑筋,代码很挫,自己也是python学习者,记录一下代码:

#-*- coding: utf-8 -*-import httplib2,xlrd,xlwt,time,jsonfrom xlutils.copy import copydef Time():    tim=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))    return timprint "test begin: "+Time()#开始时间oldwb=xlrd.open_workbook(r'url.xls')oldsh = oldwb.sheet_by_index(0)nrows=oldsh.nrowsnewwb=copy(oldwb)newsh=newwb.get_sheet(0)#第一次调用xlrd,xlwtdef GetHttpStatus(url):    try:        conn= httplib2.Http(disable_ssl_certificate_validation=True)        Start=time.time()        req=conn.request(url)        End=time.time()        diff= End-Start        return req[0],diff    except Exception as err:        return(err,diff)#https请求方法,请求时间for i in range(1,nrows):    url1=oldsh.cell_value(i,1)    url=url1    status=GetHttpStatus(url)[0]['status']    reqtime=GetHttpStatus(url)[0]    newsh.write(i,2,status)    newsh.write(i,5,Time())    newsh.write(i,6,reqtime)    if reqtime < 1.0:        newsh.write(i,7,'Normal')    else:        newsh.write(i,7,'Timeout')newwb.save('newurl.xls')#将复制过的数据保存在newurl.xlsnewwb=xlrd.open_workbook(r'newurl.xls')newsh=newwb.sheet_by_index(0)nroNws=newsh.nrowsoldwb1=copy(newwb)oldsh1=oldwb1.get_sheet(0)#第二次调用xlrd,xlwt,复制newurl.xls到url.xls进行实际结果与预期结果对比for n in range(1,nroNws):    EX_reusult=newsh.cell(n,2).value    AC_reusult=newsh.cell(n,3).value    if EX_reusult == AC_reusult:        oldsh1.write(n,4,"PASS")    else:        oldsh1.write(n,4,"FAIL")oldwb1.save('url.xls')print "test over: "+Time()#结束时间    

1 0
原创粉丝点击