zoukankan      html  css  js  c++  java
  • 更完善的接口自动化脚本

      1 #encoding:utf-8
      2 
      3 import httplib2,xlrd,xlwt,json,time,requests
      4 from xlutils.copy import copy
      5 
      6 #创建测试类
      7 class Test(object):
      8     def __init__(self,url):
      9         #self.uri = uri
     10         self.url = url
     11         
     12 
     13     
     14     
     15     #获取url内容
     16     def GetHttp(self):
     17         try:
     18             conn= httplib2.Http()
     19             #Start =time.time()
     20             req =conn.request(self.url)
     21             #End = time.time()
     22             self.status= req[0].status
     23             self.value =req[1]
     24             #self.diff = End - Start
     25             return self.status,self.value
     26         except:
     27             return(0,0)
     28 
     29     #获取URL请求时间
     30     def RequestTime(self):
     31         try:
     32             r = requests.get(self.url)
     33             ResponseTime=float(r.elapsed.microseconds)/1000
     34             return ResponseTime
     35         except:
     36             return "false"
     37             
     38     #获取json内容,抓取必要字段,address,name
     39     def JsonDetail(self,value):
     40         try:
     41             value_dic= json.loads(value)
     42             _Items = value_dic["ats"]["poi_list"]
     43             _message = value_dic["ats"]["message"].strip()
     44             poi_list =[]
     45             if _message == "Successful.":
     46                 for item in _Items:
     47                     name= item["name"].strip()
     48                     address = item["address"].strip()
     49                     poi_list.append("name:"+name+",address:"+address)
     50                 poi_list=";" .join(poi_list)
     51                 return _message
     52             else:
     53                 return _message
     54         except:
     55             return u"不是json格式"
     56 
     57 def Time():
     58         tim = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
     59         return tim
     60             
     61 def teston():
     62     oldex = xlrd.open_workbook(r"url.xls")
     63     oldsh = oldex.sheet_by_index(0)
     64     nrows = oldsh.nrows
     65     newex = copy(oldex)
     66     newsh = newex.get_sheet(0)
     67     
     68     print "Begin time:" +Time()
     69     newsh.write(1,12,Time())
     70     for i in xrange(1,nrows):
     71         #预期结果
     72         ex_result = oldsh.cell(i,0).value
     73         print ex_result
     74        # print "#%d" %i,u"83测试url"+oldsh.cell(i,1).value.strip()
     75         print "#%d" %i,u"test url"+oldsh.cell(i,2).value.strip()
     76         #实例化83 接口与 test接口
     77         #Url83 = Test(oldsh.cell(i,1).value.strip())
     78         TestUrl = Test(oldsh.cell(i,2).value.strip())
     79         #获取response和value
     80         #Get83Sta,Get83Val = Url83.GetHttp()
     81         GetTestSta,GetTestVal = TestUrl.GetHttp()
     82         #获取url请求时间
     83         #Get83Tim = Url83.RequestTime()
     84         GetTestTim = TestUrl.RequestTime()
     85         #获取选定内容
     86         #Get83Item = Url83.JsonDetail(Get83Val)
     87         GetTestItem = TestUrl.JsonDetail(GetTestVal)
     88         #if Get83Item != "Successful.":
     89          #   newsh.write(i,14,Get83Item)
     90         if GetTestItem != "Successful.":
     91             newsh.write(i,15,GetTestItem)
     92         #记录response
     93         #newsh.write(i,3,Get83Sta)
     94         newsh.write(i,4,GetTestSta)
     95         #判断83url请求状态是否为200,是写入request time和 details
     96         '''
     97         if Get83Sta == 200:
     98             newsh.write(i,5,Get83Tim)
     99             newsh.write(i,7,Get83Item)
    100             #判断83与测试服务器返回是否一致
    101             if Get83Item == GetTestItem:
    102                 newsh.write(i,9,"Same")
    103             else:
    104                 newsh.write(i,9,"Unlike")
    105             #判断预期结果
    106             if ex_result in Get83Item:
    107                 newsh.write(i,10,"PASS")
    108             else:
    109                 newsh.write(i,10,"Fail")
    110         else:
    111             newsh.write(i,3,Get83Sta)
    112             newsh.write(i,10,u"83网页错误")
    113         '''
    114         #判断testurl请求状态是否为200,是写入request time和 details
    115         if GetTestSta == 200:
    116             newsh.write(i,6,GetTestTim)
    117             newsh.write(i,8,GetTestItem)
    118             #判断预期结果
    119             if ex_result in GetTestItem:
    120                 newsh.write(i,11,"PASS")
    121             else:
    122                 newsh.write(i,11,"Fail")
    123         else:
    124             newsh.write(i,3,GetTestSta)
    125             newsh.write(i,11,u"121网页错误")
    126             
    127     print "End time:" +Time()
    128     newsh.write(1,13,Time())
    129     newex.save(r'url.xls')
    130     
    131 if __name__ == "__main__":
    132     teston()

    这是其中一种羡慕的实践代码,分别对比两个api 返回结果,查看返回时间,以及response code。原理比之前类似,但代码更清楚规范

    以下是excel中对应的结果

  • 相关阅读:
    oracle查询哪些sp修改了某些表
    asp.net mvc
    更新计算机驱动
    instr函数的用法
    UNION ALL UNION
    Python机器学习ch02 代码学习2
    Python机器学习 ch02代码学习1
    转载Python切片(小知识点)
    FMCW部分资料连接
    Python基础25 异常堆栈跟踪,释放资源,自定义异常和主动抛出
  • 原文地址:https://www.cnblogs.com/BUGU/p/4739272.html
Copyright © 2011-2022 走看看