下面是soa接口調用的核心代碼
#! /usr/bin/python # coding:utf-8
from suds.client import Client
def SoaRequest(wsdl,fnname,data): soaService = Client(wsdl).service soaRep = getattr(soaService,fnname)(data) return soaRep
問題就這樣出現了:
我調用一個接口,總是報錯,見下圖:
之后Debug斷點定位到suds模塊的sxbasic.py文件中的Import類的open方法
def open(self, options):""" Open and import the refrenced schema. @param options: An options dictionary. @type options: L{options.Options} @return: The referenced schema. @rtype: L{Schema} """
if self.opened: return self.opened = True log.debug('%s, importing ns="%s", location="%s"', self.id, self.ns[1], self.location) result = self.locate() if result is None: if self.location is None: log.debug('imported schema (%s) not-found', self.ns[1]) else:
result = self.download(options) log.debug('imported:\n%s', result) return result
self.location 為None時日志打印,否則就執行download
而我測試的接口有個這種問題,A中依賴B,B中依賴C,C中依賴A,循環依賴
運行后控制台就會報錯:
RuntimeError: maximum recursion depth exceeded
是不是遞歸深度不夠呢(個人覺得循環的話,深度再大有什么用了)
import sys sys.setrecursionlimit(1000000)
設置100萬的遞歸深度,再運行直接 stack overflow,溢出了
好吧,那就改吧
在類Import外定義數組變量resultList = []
def open(self, options): global resultList
if self.opened: return self.opened = True log.debug('%s, importing ns="%s", location="%s"', self.id, self.ns[1], self.location) result = self.locate() if result is None: if self.location is None: log.debug('imported schema (%s) not-found', self.ns[1]) else: if self.location in resultList: #若location存在List中,則打印debug日志,不加載options log.debug('location is already in resultList') else: resultList.append(self.location) #location不存在時,List中append后,加載options result = self.download(options) log.debug('imported:\n%s', result) return result
去除了循環依賴,出現另外一個問題:
先看下我soap接口的通用調用方法:
#! /usr/bin/python # coding:utf-8 import sys reload(sys) sys.setdefaultencoding( "utf-8" ) from suds.client import Clientdef SoaRequest(wsdl,fnname,data): #soap接口調用方法 soaService = Client(wsdl).service soaRep = getattr(soaService,fnname)(data) return soaRep
通過這個方法去逐行調用excel中的用例參數,來實現數據驅動的接口自動化框架
但是單個接口,多組參數用例的組合場景,運用上面的腳本后就報錯
Type not found.......
考慮到可能是因為第一行用例執行過程中,在List中append 進location,到執行第二條用例的時候,由於和第一行用例的接口是一樣的,故而List判斷中就沒有進入到download分支,所以就拋出了not found的異常
解決方法是在執行用例前,清空list
改造soap接口核心調用程序
#! /usr/bin/python # coding:utf-8 import sys reload(sys) sys.setdefaultencoding( "utf-8" ) from suds.client import Client from suds.xsd import sxbasic def SoaRequest(wsdl,fnname,data): #soap接口調用方法 sxbasic.resultList=[] #初始化location列表 soaService = Client(wsdl).service soaRep = getattr(soaService,fnname)(data) return soaRep
這樣就萬事大吉了
如果有人有更好的解決方案,更簡便的處理方法,歡迎溝通交流 本人郵箱:270193176@qq.com