记 suds 模块循环依赖的坑-RuntimeError: maximum recursion depth exceeded

下面是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

posted on 2016-04-18 17:28  涛哥爱吃面  阅读(3890)  评论(0编辑  收藏  举报

导航