zoukankan      html  css  js  c++  java
  • python 自动化测试HTTP接口

    获取请求code,并把请求结果进行对比写到excel里,效率低一些,但是可以执行并有效的验证。此python脚本是自己瞎琢磨而成,有同事的优化帮忙,只是记录一下成果。代码如下

    #-*- coding: utf-8 -*-
    
    import httplib2,xlrd,xlwt,time,json
    from xlutils.copy import copy
    
    def Time():
        tim=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        return tim
    
    print "test begin: "+Time()
    #开始时间
    
    oldwb=xlrd.open_workbook(r'url.xls')
    oldsh = oldwb.sheet_by_index(0)
    nrows=oldsh.nrows
    newwb=copy(oldwb)
    newsh=newwb.get_sheet(0)
    #第一次调用xlrd,xlwt
    
    def 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)[1]
        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')
        AC_reusult=oldsh.cell(i,2).value
        EX_reusult=oldsh.cell(i,3).value
        if AC_reusult == EX_reusult:
            newsh.write(i,4,"PASS")
        else:
            newsh.write(i,4,"FAIL")
    newwb.save('url.xls')
    #将复制过的数据保存在newurl.xls
    
    
    print "test over: "+Time()
    #结束时间
        
    

     

  • 相关阅读:
    单例类
    UITableView汇总
    JVM虚拟机——垃圾收集算法
    集合框架 HashMap 的扩容机制, ConcurrnetHashMap 的原理
    java判断两个单链表是否相交
    java中Comparator 和 Comparable的区别
    String getProperty(String key, String def)
    JAVA 单步调试快捷键
    Stanford Word Segmenter使用
    使用ifstream和getline读取文件内容[c++]
  • 原文地址:https://www.cnblogs.com/BUGU/p/4253221.html
Copyright © 2011-2022 走看看