python 接口自动化测试
Posted 涛哥爱吃面
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 接口自动化测试相关的知识,希望对你有一定的参考价值。
代码实现
1.XlsEngine.py
# -*- coding:utf-8 -*- __author__ = ‘yanghaitao‘ import xlrd import xlwt class XlsEngine_rd(): """ The XlsEngine is a demo class for excel openration Just for some basic test or the using or the 3rd class in python """ def __init__(self,file): # define class variable self.xls_name = file self.xlrd_object = None self.xlwt_object = None self.isopenfailed = True def xlrd_open(self): try: # xlrd.Book.encoding="utf-8" self.xlrd_object = xlrd.open_workbook(self.xls_name) self.isopenfailed = False pass except Exception,e: self.isopenfailed = True self.xlrd_object = None print(e) pass finally: ‘‘‘ do nothing ‘‘‘ pass return [self.isopenfailed,self.xlrd_object] def dump_sheet(self): if self.isopenfailed == False: ‘‘‘ dump the sheet usging for getting the sheet table = data.sheets()[0] table = data.sheet_by_index(0) table = data.sheet_by_name(u‘Sheet1‘) ‘‘‘ for name in self.xlrd_object.sheet_names(): table = self.xlrd_object.sheet_by_name(name) print("sheet %s rownums=%d colnums=%d"%(name,table.nrows,table.ncols)) else: print("file %s is not open \n"%self.xls_name) def dump_cell(self,sheet_index,cell_row,cell_col): ‘‘‘ sheet_index cell_row cell_col ‘‘‘ try: table=self.xlrd_object.sheet_by_index(0) value=table.cell(cell_row,cell_col).value return value pass except: print(‘dump_cell,error‘) class XlsEngine_wt(): def __init__(self,file): self.xls_name = file self.xlwt_object = None self.xlwt_sheet = None self.isopenfailed = True def open(self): try: self.xlwt_object = xlwt.Workbook() self.isopenfailed = False except Exception,e: print e def add_sheet(self,sheet_name): ‘‘‘ Create a sheet to the xls ‘‘‘ try: self.xlwt_object = xlwt.Workbook() self.xlwt_sheet = self.xlwt_object.add_sheet(sheet_name,cell_overwrite_ok=True) self.xlwt_sheet.write(0,0,‘WSDL‘) self.xlwt_sheet.write(0,1,u‘方法‘) self.xlwt_sheet.write(0,2,‘DATA‘) self.xlwt_sheet.write(0,3,u‘预期结果‘) self.xlwt_sheet.write(0,4,u‘执行结果‘) except Exception,e: print(e) def get_sheet(self,sheet_index): try: self.xlwt_object = xlwt.Workbook() self.xlwt_sheet = self.xlwt_object.get_sheet(sheet_index) except Exception,e: print(e) def write(self,row,col,value): ‘‘‘ Write value to (row,col) ‘‘‘ try: self.xlwt_sheet.write(row,col,value) except Exception,e: print(e) def save_xls(self): ‘‘‘ Save the change to xlsfile ‘‘‘ try: self.xlwt_object.save(self.xls_name) except Exception,e: print(e) def set_fontColour(self,colour_index): fnt=xlwt.Font() fnt.colour_index=colour_index fnt.bold=True style=xlwt.XFStyle() style.font=fnt return style def write_result(self,row,list,index_list,as_value=True): ‘‘‘ row 行,list 结果列表,index_list 填写数据列表中的值的索引,wsdl 结果中显示调用接口地址,as_value 预期结果 ‘‘‘ self.write(row,0,str(list[index_list][0])) self.write(row,1,str(list[index_list][1])) self.write(row,2,str(list[index_list][2])) self.write(row,3,bool(list[index_list][3])) if(bool(list[index_list][3])==as_value): #预期结果是否为True self.xlwt_sheet.write(row,4,str(as_value)+‘_Success‘,self.set_fontColour(3)) #colour_index=3,绿色,通过 else: self.xlwt_sheet.write(row,4,str(as_value)+‘_Fail‘,self.set_fontColour(2)) #colour_index=2,红色,失败 def write_resultRed(self,row,list,index_list,wsdl): self.write(row,0,str(list[index_list][0])) #WSDL地址 self.write(row,1,str(list[index_list][1])) #方法名 self.write(row,2,str(list[index_list][2])) #传入参数(用例) self.write(row,3,bool(list[index_list][3])) #预期结果 self.xlwt_sheet.write(row,4,‘Unkonw_Fail‘,self.set_fontColour(2)) #colour_index=2,红色,失败(执行结果)
2.VIPSoap.py
#! /usr/bin/python # coding:utf-8 from suds.client import Client class Service: def __init__(self,wsdl): self.client=Client(wsdl) def Method(self,dict): result=self.client.service.Method(dict) return result
3.DataEngine.py
__author__ = ‘yanghaitao‘ from XlsEngine import XlsEngine_rd import Logging COUNT_ROWs=1 def data2List(file,sheet_index): data = XlsEngine_rd(file) data.xlrd_open() sheet = data.xlrd_object.sheet_by_index(sheet_index) rows = sheet.nrows result_list=[] for i in range(rows): if(i != 0): result_list.append(sheet.row_values(i)) return result_list def resultCheck(test_rep,xlw,list,xls_row): global COUNT_ROWs if(test_rep.Success == True): xlw.write_result(COUNT_ROWs,list,xls_row,True) COUNT_ROWs=COUNT_ROWs+1 elif(test_rep.Success == False): Logging.writeLog(str(list[xls_row][1]),test_rep) xlw.write_result(COUNT_ROWs,list,xls_row,False) COUNT_ROWs=COUNT_ROWs+1 else: Logging.writeLog(str(list[xls_row][1]),test_rep) xlw.write_resultRed(COUNT_ROWs,list,xls_row) COUNT_ROWs=COUNT_ROWs+1
4.Logging.py
#! /usr/bin/python # coding:utf-8 import logging,time import sys reload(sys) sys.setdefaultencoding( "utf-8" ) logging.basicConfig(level=logging.INFO, format=‘%(asctime)s %(filename)s[line:%(lineno)d] 【%(levelname)s】 %(message)s‘, datefmt=‘%a, %d %b %Y %H:%M:%S‘, filename=r".\Log\Service"+time.strftime(r‘%Y-%m-%d‘, time.localtime(time.time()))+".log", filemode=‘a‘) console = logging.StreamHandler() logging.getLogger(‘suds.client‘).addHandler(console) def writeLog(methodname,result): ‘‘‘写日志‘‘‘ content = methodname + "\n" for item in result: content=content+‘\t|‘+str(item) if result.Success==False: logging.error(content) def writeException(msg): ‘‘‘写日志‘‘‘ logging.error("【Catch Exception】"+str(msg))
以上是关于python 接口自动化测试的主要内容,如果未能解决你的问题,请参考以下文章
pytest接口自动化测试框架 | 用python代码测试接口
Python接口测试自动化说明及代码实例:含getpostputdelete等方法