1 #Encoding:utf-82 3 Importhttplib2,xlrd,xlwt,json,time,requests4 fromXlutils.copyImportCopy5 6 #To create a test class7 classTest (object):8 def __init__(self,url):9 #Self.uri = UriTenSelf.url =URL One A - - the #Get URL content - defgethttp (self): - Try: -conn=Httplib2. Http () + #Start =time.time () -req =conn.request (Self.url) + #End = Time.time () Aself.status=Req[0].status atSelf.value =req[1] - #Self.diff = End-start - returnSelf.status,self.value - except: - return(0,0) - in #get URL Request time - defRequestTime (self): to Try: +R =requests.get (Self.url) -Responsetime=float (r.elapsed.microseconds)/1000 the returnResponseTime * except: $ return "false"Panax Notoginseng - #get JSON content, fetch necessary fields, Address,name the defJsondetail (self,value): + Try: AValue_dic=json.loads (value) the_items = value_dic["ATS"]["poi_list"] +_message = value_dic["ATS"]["message"].strip () -Poi_list =[] $ if_message = ="successful.": $ forIteminch_items: -Name= item["name"].strip () -Address = item["Address"].strip () thePoi_list.append ("Name:"+name+", Address:"+address) -poi_list=";". Join (poi_list)Wuyi return_message the Else: - return_message Wu except: - returnU"not in JSON format" About $ defTime (): -Tim = Time.strftime ('%y-%m-%d%h:%m:%s', Time.localtime (Time.time ())) - returnTim - A defTeston (): +Oldex = Xlrd.open_workbook (r"Url.xls") theOLDSH =oldex.sheet_by_index (0) -nrows =oldsh.nrows $Newex =copy (Oldex) theNewsh =newex.get_sheet (0) the the Print "Begin Time:"+Time () theNewsh.write (1,12, Time ()) - forIinchXrange (1, nrows): in #Expected results theEx_result =Oldsh.cell (i,0). Value the PrintEx_result About #print "#%d"%i,u "83 test URL" +oldsh.cell (i,1). Value.strip () the Print "#%d"%i,u"Test URL"+oldsh.cell (i,2). Value.strip () the #instantiate the 83 interface with the test interface the #Url83 = Test (Oldsh.cell (i,1). Value.strip ()) +Testurl = Test (Oldsh.cell (i,2). Value.strip ()) - #get response and value the #get83sta,get83val = url83.gethttp ()BayiGetteststa,gettestval =testurl.gethttp () the #get URL Request time the #Get83tim = Url83.requesttime () -Gettesttim =testurl.requesttime () - #Get Selection the #Get83item = Url83.jsondetail (get83val) theGettestitem =Testurl.jsondetail (gettestval) the #if Get83item! = "successful.": the #newsh.write (I,14,get83item) - ifGettestitem! ="successful.": theNewsh.write (i,15, Gettestitem) the #Record Response the #newsh.write (I,3,get83sta)94Newsh.write (i,4, Getteststa) the #determines whether the 83url request state is 200, which is written to request time and details the " " the if Get83sta = =:98 newsh.write (I,5,get83tim) About newsh.write (I,7,get83item) - #判断83与测试服务器返回是否一致101 if Get83item = = Gettestitem:102 Newsh.write (i,9, "Same")103 Else:104 Newsh.write (i,9, "unlike") the #判断预期结果106 if Ex_result in Get83item:107 Newsh.write (i,10, "PASS")108 Else:109 Newsh.write (i,10, "Fail") the Else:111 newsh.write (I,3,get83sta) the Newsh.write (i,10,u "83 page error")113 " " the #determines whether the Testurl request state is 200, which is written to request time and details the ifGetteststa = = 200: theNewsh.write (i,6, Gettesttim)117Newsh.write (i,8, Gettestitem)118 #Judging the expected results119 ifEx_resultinchGettestitem: -Newsh.write (i,11,"PASS")121 Else:122Newsh.write (i,11,"Fail")123 Else:124Newsh.write (i,3, Getteststa) theNewsh.write (i,11,u"121 Page Error")126 127 Print "End Time:"+Time () -Newsh.write (1,13, Time ())129Newex.save (R'Url.xls') the 131 if __name__=="__main__": theTeston ()
This is one of the envy of the practice code, comparing two API return results, check the return time, as well as response code. The principle is similar to the previous, but the code is more clearly regulated
The following are the corresponding results in Excel
More sophisticated interface automation scripts