这个代码是干什么的呢?
读取一个Excel中的一条数据用例,请求接口,然后返回结果并反填到excel中。过程中会生成请求回来的文本,当然还会生成一个xml文件。具体的excel文件如下:
ID 测试要点 CityName CountryName 期望值 实际值 结果 响应时间
1 北京天气 beijing china Success
2 上海天气 shanghai china Success
读取一个Excel中的一条数据用例,请求接口,然后返回结果并反填到excel中。过程中会生成请求回来的文本,当然还会生成一个xml文件。具体的excel文件如下:
ID 测试要点 CityName CountryName 期望值 实际值 结果 响应时间
1 北京天气 beijing china Success
2 上海天气 shanghai china Success
# -*- coding: UTF-8 -*- |
002 | from xml.dom import minidom |
003 | import xlrd |
004 | import openpyxl |
005 | import requests |
006 | import json |
007 | import sys |
008 | import HTMLParser |
009 | import os |
010 | import re |
011 | import codecs |
012 | import time |
013 | import datetime |
014 | |
015 | reload(sys) |
016 | sys.setdefaultencoding('utf-8') |
017 | |
018 | class OptionExcelData(object): |
019 | """对Excel进行操作,包括读取请求参数,和填写操作结果""" |
020 | def __init__(self, excelFile,excelPath=''): |
021 | self.excelFile = excelFile |
022 | self.excelPath = excelPath |
023 | self.caseList = [] |
024 | |
025 | """ |
026 | 传入:传入用例Excel名称 |
027 | 返回:[],其中元素为{},每个{}包含行号、城市、国家和期望结果的键值对 |
028 | """ |
029 | def getCaseList(self,excelFile,excelPath=''): |
030 | readExcel = xlrd.open_workbook(fileName) #读取指定的Excel |
031 | try: |
032 | table = readExcel.sheet_by_index(0) #获取Excel的第一个sheet |
033 | trows = table.nrows #获取Excel的行数 |
034 | for n in range(1,trows): |
035 | tmpdict = {} #把一行记录写进一个{} |
036 | tmpdict['id'] = n #n是Excel中的第n行 |
037 | tmpdict['CityName'] = table.cell(n,2).value |
038 | tmpdict['CountryName'] = table.cell(n,3).value |
039 | tmpdict['Rspect'] = table.cell(n,4).value |
040 | self.caseList.append(tmpdict) |
041 | except Exception, e: |
042 | raise |
043 | finally: |
044 | pass |
045 | return self.caseList |
046 | |
047 | """ |
048 | 传入:请求指定字段结果,是否通过,响应时间 |
049 | 返回: |
050 | """ |
051 | def writeCaseResult(self,resultBody,isSuccess,respTime,\\ |
052 | excelFile,theRow,theCol=5): |
053 | writeExcel = openpyxl.load_workbook(excelFile) #加载Excel,后续写操作 |
054 | try: |
055 | wtable = writeExcel.get_sheet_by_name('Sheet1') #获取名为Sheet1的sheet |
056 | wtable.cell(row=theRow+1,column=theCol+1).value = resultBody #填写实际值 |
057 | wtable.cell(row=theRow+1,column=theCol+2).value = isSuccess #填写是否通过 |
058 | wtable.cell(row=theRow+1,column=theCol+3).value = respTime #填写响应时间 |
059 | writeExcel.save(excelFile) |
060 | except Exception, e: |
061 | raise |
062 | finally: |
063 | pass |
064 | |
065 | |
066 | class GetWeather(object): |
067 | """获取天气的http请求""" |
068 | def __init__(self, serviceUrl,requestBody,headers): |
069 | self.serviceUrl = serviceUrl |
070 | self.requestBody = requestBody |
071 | self.headers = headers |
072 | self.requestResult = {} |
073 | |
074 | """ |
075 | 传入:请求地址,请求体,请求头 |
076 | 返回:返回{},包含响应时间和请求结果的键值对 |
077 | """ |
078 | def getWeath(self,serviceUrl,requestBody,headers): |
079 | timebefore = time.time() #获取请求开始的时间,不太严禁 |
080 | tmp = requests.post(serviceUrl,data=requestBody,\\ |
081 | headers=headers) |
082 | timeend = time.time() #获取请求结束的时间 |
083 | tmptext = tmp.text |
084 | self.requestResult['text'] = tmptext #记录响应回来的内容 |
085 | self.requestResult['time'] = round(timeend - timebefore,2) #计算响应时间 |
086 | return self.requestResult |
087 | |
088 | class XmlReader: |
089 | """操作XML文件""" |
090 | def __init__(self,testFile,testFilePath=''): |
091 | self.fromXml = testFile |
092 | self.xmlFilePath = testFilePath |
093 | self.resultList = [] |
094 | |
095 | def writeXmlData(self,resultBody,testFile,testFilePath=''): |
096 | tmpXmlFile = codecs.open(testFile,'w','utf-16') #新建xml文件 |
097 | tmpLogFile = codecs.open(testFile+'.log','w','utf-16') #新建log文件 |
098 | |
099 | tmp1 = re.compile(r'\\<.*?\\>') #生成正则表达式:<*?> |
100 | tmp2 = tmp1.sub('',resultBody['text']) #替换相应结果中的<*?> |
101 | html_parser = HTMLParser.HTMLParser() |
102 | xmlText = html_parser.unescape(tmp2) #转换html编码 |
103 | |
104 | try: |
105 | tmpXmlFile.writelines(xmlText.strip()) #去除空行并写入xml |
106 | tmpLogFile.writelines('time: '+\\ |
107 | str(resultBody['time'])+'\\r\\n') #把响应时间写入log |
108 | tmpLogFile.writelines('text: '+resultBody['text'].strip()) #把请求回来的文本写入log |
109 | except Exception, e: |
110 | raise |
111 | finally: |
112 | tmpXmlFile.close() |
113 | tmpLogFile.close() |
114 | |
115 | """返回一个list""" |
116 | def readXmlData(self,testFile,testFilePath=''): |
117 | tmpXmlFile = minidom.parse(testFile) |
118 | root = tmpXmlFile.documentElement |
119 | tmpValue = root.getElementsByTagName('Status')[0].\\ |
120 | childNodes[0].data |
121 | return tmpValue #获取特定字段并返回结果,此处选取Status |
122 | |
123 | |
124 | if __name__ == '__main__': |
125 | |
126 | requesturl = 'http://www.webservicex.net/globalweather.asmx/GetWeather' |
127 | requestHeadrs = {"Content-Type":"application/x-www-form-urlencoded"} |
128 | fileName = u'用例内容.xlsx' |
129 | |
130 | ed = OptionExcelData(fileName) |
131 | testCaseList = ed.getCaseList(ed.excelFile) |
132 | |
133 | for caseDict in testCaseList: |
134 | caseId = caseDict['id'] |
135 | cityName = caseDict['CityName'] |
136 | countryName = caseDict['CountryName'] |
137 | rspect = caseDict['Rspect'] |
138 | requestBody = 'CityName='+cityName+'&CountryName='+countryName |
139 | |
140 | getWeather = GetWeather(requesturl,requestBody,requestHeadrs) |
141 | #获取请求结果 |
142 | tmpString = getWeather.getWeath(getWeather.serviceUrl,\\ |
143 | getWeather.requestBody,getWeather.headers) |
144 | |
145 | xd = XmlReader(str(caseId) + '.xml') |
146 | #把请求内容写入xml和log |
147 | xd.writeXmlData(tmpString,xd.fromXml) |
148 | response = xd.readXmlData(str(caseId) + '.xml') |
149 | respTime = tmpString['time'] |
150 | if response == rspect: |
151 | theResult = 'Pass' |
152 | else: |
153 | theResult = 'False' |
154 | |
155 | ed.writeCaseResult(response,theResult,respTime,fileName,caseId) |
文章来源:学什么网
本文介绍了一个用于自动化测试的Python框架,该框架能够从Excel中读取测试案例,并自动执行HTTP请求,将响应结果及测试结论回写到Excel中,同时记录详细的请求响应信息到XML文件。适用于接口自动化测试场景。
4171

被折叠的 条评论
为什么被折叠?



