简单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 表格样式,请大家自行对照

posted @ 2015-07-02 18:06  虫子宴  阅读(1391)  评论(0编辑  收藏  举报