web接口测试框架简单实现 python
上一篇 / 下一篇 2017-07-11 05:55:14 / 个人分类:测试积累
Python实现
importxlrd
importopenpyxl
importrequests
importtime
importrequests
importre
importjson
classReadWriteExcelData(object):
# initial excel object
def __init__(self,excelFile,excelPath=''):
self.excelFile=excelFile
self.excelPath=excelPath
self.caseList=[]
# read test case
defgetCaseList(self,excelFile,excelPath=''):
readExcel=xlrd.open_workbook(fileName)
try:
table=readExcel.sheet_by_index(0)
trows=table.nrows
forninrange(1,trows):
tmpdict={}
tmpdict['No.']=table.cell(n,0).value
tmpdict['TestCase']=table.cell(n,1).value
tmpdict['url']=table.cell(n,2).value
tmpdict['args']=table.cell(n,3).value
tmpdict['method']=table.cell(n,4).value
tmpdict['E_code']=table.cell(n,5).value
tmpdict['E_text']=table.cell(n,6).value
self.caseList.append(tmpdict)
except(Exception):
raise
finally:
pass
returnself.caseList
# write test result
defwriteCaseResult(self,A_code,A_text,resp_time,test_result,exec_string,excelFile,theRow,theCol=7):
writeExcel=openpyxl.load_workbook(excelFile)
try:
# wtable = writeExcel.get_sheet_by_name('Sheet1')
sheetname=writeExcel.get_sheet_names()[0]
wtable=writeExcel.get_sheet_by_name(sheetname)
wtable.cell(row=theRow+1,column=theCol+1).value=A_code
wtable.cell(row=theRow+1,column=theCol+2).value=A_text
wtable.cell(row=theRow+1,column=theCol+3).value=resp_time
wtable.cell(row=theRow+1,column=theCol+4).value=test_result
wtable.cell(row=theRow+1,column=theCol+5).value=exec_string
writeExcel.save(excelFile)
except(Exception):
raise
finally:
pass
if__name__=='__main__':
fileName=r'test.xlsx'
# read test case information
ed=ReadWriteExcelData(fileName)
testCaseList=ed.getCaseList(ed.excelFile)
# print(testCaseList)
forcaseDictintestCaseList:
case_Id=int(caseDict['No.'])
case_TestCase=caseDict['TestCase']
case_url=caseDict['url']
case_args=caseDict['args']
case_method=caseDict['method']
case_E_code=caseDict['E_code']
case_E_text=caseDict['E_text'].replace(r'\n','')
# to build request command
pattern=r'^\s*data\s*=\s*(.+)$'
params_pat=r'params\s*='
ifcase_args=='':
exec_string="requests.{method}('{url}')".format(method=case_method,url=case_url)
elifre.match(pattern,case_args):
value=re.match(pattern,case_args).group(1).strip()
case_args="data = json.dumps({})".format(value)
exec_string="requests.{method}('{url}',{args})".format(method=case_method,url=case_url,args=case_args)
else:
exec_string="requests.{method}('{url}',{args})".format(method=case_method,url=case_url,args=case_args)
print('#{} --- request: {}'.format(case_Id,exec_string))
# execute request and get spend time
timebefore=time.time()
r=eval(exec_string)
timeend=time.time()
timecost=round(timeend-timebefore,2)
A_code=r.status_code
A_text=r.text
resp_time='{} sec'.format(str(timecost))
# special for json null
# check point for response and status code
null=None
ifcase_E_text:
actual_dict=eval(json.loads(json.dumps(A_text)))
expect_dict=eval(case_E_text)
flag=True
forkeyinactual_dict:
ifkeyin['origin']:
break
elifre.search(params_pat, case_args)andkeyin['url']:
break
elifactual_dict.get(key,None)!=expect_dict.get(key,None):
flag=False
ifA_code==case_E_codeandflag==True:
test_result='Pass'
else:
test_result='False'
print(type(eval(case_E_text)))
print(eval(case_E_text))
print(type(eval(json.loads(json.dumps(A_text)))))
print(eval(json.loads(json.dumps(A_text))))
else:
ifA_code==case_E_code:
test_result='Pass'
else:
test_result='False'
print('Cast #{} done, status: {}'.format(case_Id,test_result))
# write the actual value and test result back into the excel
ed.writeCaseResult(A_code,A_text,resp_time,test_result,exec_string,fileName,case_Id)
Excel格式:
No. TestCase url args method E_code E_text A_code A_text resp_time test_result exec_string 1 Get_Demo https://github.com/timeline.json get 410 {"message":"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"} 2 Post_Demo http://httpbin.org/post post 200 {\n "args": {}, \n "data": "", \n "files": {}, \n "form": {}, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Content-Length": "0", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.14.2"\n }, \n "json": null, \n "origin": "112.64.217.75", \n "url": "http://httpbin.org/post"\n}\n 3 Put_Demo http://httpbin.org/put put 200 {\n "args": {}, \n "data": "", \n "files": {}, \n "form": {}, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Content-Length": "0", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.14.2"\n }, \n "json": null, \n "origin": "112.64.217.75", \n "url": "http://httpbin.org/put"\n}\n 4 Delete_Demo http://httpbin.org/delete delete 200 {\n "args": {}, \n "data": "", \n "files": {}, \n "form": {}, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Content-Length": "0", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.14.2"\n }, \n "json": null, \n "origin": "112.64.217.75", \n "url": "http://httpbin.org/delete"\n}\n 5 Head_Demo http://httpbin.org/get head 200 6 Options_Demo http://httpbin.org/get options 200 7 Url_Args_Demo http://httpbin.org/get params={'key1': 'value1', 'key2': 'value2'} get 200 {\n "args": {\n "key1": "value1", \n "key2": "value2"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Connection": "close", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.14.2"\n }, \n "origin": "140.207.223.149", \n "url": "http://httpbin.org/get?key1=value1&key2=value2"\n}\n 8 data_Args_Demo https://github.com/timeline.json data = {'some': 'data'} get 410 {"message":"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}
TAG:
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 36451
- 日志数: 55
- 建立时间: 2014-06-19
- 更新时间: 2017-08-29