zoukankan      html  css  js  c++  java
  • 简单python脚本,配合excel表格,实现接口自动化测试

    #encoding: utf-8
    
    import urllib2,xlrd,xlwt,time
    from xlutils.copy import copy
    from lxml import etree
    
    def Time():
        tim=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        return tim
    
    class Test(object):
    
        def __init__(self,url,uri):
            self.url=url
            self.uri=uri
    
        def openurl(self):
            try:
                response=urllib2.urlopen(self.url)
                self.html=response.read()
                return ("pass",self.html)
            except urllib2.URLError,e:
                ex=e.code+e.reason
                return ("except",ex)
        #测试用例-添加方法
        def case(self,EC_result,html):
            self.EC_result =EC_result
            root = etree.fromstring(html)
            Message=root.xpath('/ats/message')[0].text
            valuelist=[]
            if Message ==  "Successful.":
                __VALUE=root.xpath(self.uri)
                for val in __VALUE:
                    value=val.text
                    valuelist.append(value)return "PASS"
                else:
                    return "FALSE"
            else:
                return Message
        #使用len进行对比
        def caselen(self,EC_result,html):
            self.EC_result =EC_result
            root = etree.fromstring(self.html)
            Message=root.xpath('/ats/message')[0].text  
            if Message ==  "Successful.":
                __VALUE=root.xpath(self.uri)
                if len(__VALUE) > EC_result :
                    return "PASS"
                else:
                    return "FALSE"
            else:
                return Message
        #获取元素方法
        def caseele(self,EC_result,html):
            self.EC_result =EC_result
            root = etree.fromstring(self.html)
            Message=root.xpath('/ats/message')[0].text
            
            valuelist=[]
    
            if Message ==  "Successful.":
                __VALUE=root.xpath(self.uri)
                for val in __VALUE:
                    valuelist.append(val)
                    strvalue=";".join(valuelist)
                if EC_result in strvalue:
                    return "PASS"
                else:
                    return "FALSE"
            else:
                return Message
    
    
    oldex = xlrd.open_workbook(r'ClasTtestcase.xls')
    oldsh = oldex.sheet_by_index(0)
    nrows = oldsh.nrows
    newex = copy(oldex)
    newsh = newex.get_sheet(0)
    newsh.write(1,6,Time())
    
    #dict={"traffic/item":caseA_traffic_round,
    for i in xrange(1,nrows):
        #输出LOG
        print "#%d."%i,oldsh.cell(i,0).value.strip(),u"验证"
        print "url:",oldsh.cell(i,1).value.strip()
        #实例化Testcase
        TestCase = Test(oldsh.cell(i,1).value.strip(),oldsh.cell(i,4).value.strip())
        #实际结果
        EC_result=oldsh.cell(i,2).value
        #判断赋值
        Path=oldsh.cell(i,5).value
        #异常
        status,html=TestCase.openurl()
        #条件判断
        if status == 'pass':
            if 'len' in Path:
                newsh.write(i,3,TestCase.caselen(EC_result,html))
            elif '@' in Path:
                newsh.write(i,3,TestCase.caseele(EC_result,html))
            else:
                newsh.write(i,3,TestCase.case(EC_result,html))
        else:
            newsh.write(i,3,html)
    newex.save(r'ClasTtestcase.xls')
    print u'测试完成'
    excel 表格样式,请大家自行对照

  • 相关阅读:
    linux sysfs (2)
    微软——助您启动云的力量网络虚拟盛会
    Windows Azure入门教学系列 全面更新啦!
    与Advanced Telemetry创始人兼 CTO, Tom Naylor的访谈
    Windows Azure AppFabric概述
    Windows Azure Extra Small Instances Public Beta版本发布
    DataMarket 一月内容更新
    和Steve, Wade 一起学习如何使用Windows Azure Startup Tasks
    现实世界的Windows Azure:与eCraft的 Nicklas Andersson(CTO),Peter Löfgren(项目经理)以及Jörgen Westerling(CCO)的访谈
    正确使用Windows Azure 中的VM Role
  • 原文地址:https://www.cnblogs.com/BUGU/p/4616767.html
Copyright © 2011-2022 走看看