在测试工作中经常要对两个dict进行校验,而且dict可能是多层嵌套的,为此写了个递归函数。工作方便多了
代码如下
知识点:递归函数中必须有全局变量(C++中传引用的变量),所以在python中使用字典来保存每次递归结果
如例子中的dicresult.
#递归函数
def _assertdict(expobj=None,actobj=None,dicresult=None,key=None):
spacestr = " "
bresult = dicresult['result']
if (type(actobj) != type(expobj)):
tempstr = "actobj's type not equal expobj's type and actobj's type is %s expobj's type is %s"%(type(actobj),type(expobj))
bresult = bresult and False
dicresult['result'] = bresult
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
if (type(actobj) == dict) and (type(expobj) == dict):
for e in actobj:
a_dict = actobj[e]
if e in expobj:
e_dict = expobj[e]
_assertdict(a_dict,e_dict,dicresult,e)
else:
tempstr = spacestr + "not find sub dict key is",e
bresult = bresult and False
dicresult['result'] = bresult
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
elif(type(actobj) == list) and (type(expobj) == list):
if len(actobj) != len(expobj):
tempstr = spacestr + " key%s's len is not equal"%(key)
bresult = bresult and False
dicresult['result'] = bresult
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
return
for i in range(len(actobj)):
a_list = actobj[i]
def _assertdict(expobj=None,actobj=None,dicresult=None,key=None):
spacestr = " "
bresult = dicresult['result']
if (type(actobj) != type(expobj)):
tempstr = "actobj's type not equal expobj's type and actobj's type is %s expobj's type is %s"%(type(actobj),type(expobj))
bresult = bresult and False
dicresult['result'] = bresult
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
if (type(actobj) == dict) and (type(expobj) == dict):
for e in actobj:
a_dict = actobj[e]
if e in expobj:
e_dict = expobj[e]
_assertdict(a_dict,e_dict,dicresult,e)
else:
tempstr = spacestr + "not find sub dict key is",e
bresult = bresult and False
dicresult['result'] = bresult
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
elif(type(actobj) == list) and (type(expobj) == list):
if len(actobj) != len(expobj):
tempstr = spacestr + " key%s's len is not equal"%(key)
bresult = bresult and False
dicresult['result'] = bresult
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
return
for i in range(len(actobj)):
a_list = actobj[i]
e_list = expobj[i]
if a_list != a_list:
tempstr = spacestr + " key:%s[%d]'s value is not equal"%(key,i)
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
_assertdict(a_list,e_list,dicresult,key+'[%d]'%i)
else:
if actobj == expobj:
bresult = bresult and True
dicresult['result'] = bresult
else:
if key!=None:
tempstr = spacestr + "assert key: %s error and act value is %s and exp value is %s"%(key,actobj,expobj)
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
bresult = bresult and False
dicresult['result'] = bresult
if a_list != a_list:
tempstr = spacestr + " key:%s[%d]'s value is not equal"%(key,i)
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
_assertdict(a_list,e_list,dicresult,key+'[%d]'%i)
else:
if actobj == expobj:
bresult = bresult and True
dicresult['result'] = bresult
else:
if key!=None:
tempstr = spacestr + "assert key: %s error and act value is %s and exp value is %s"%(key,actobj,expobj)
dicresult['logstr'] = dicresult['logstr'] + '/n' + tempstr
bresult = bresult and False
dicresult['result'] = bresult
调用封装的函数
def assertdict(params,expobj=None,actobj=None,msg=None):
dicresult = {}
dicresult['logstr'] = ""
dicresult['result'] = True
_assertdict(expobj,actobj,dicresult)
print dicresult
if dicresult['result']:
asserttrue(params,"assert dict success" + dicresult['logstr'],msg) //框架自带的断言函数,可忽略
else:
assertfalse(params,"assert dict failed" + dicresult['logstr'],msg) //框架自带的断言函数,可忽略
dicresult = {}
dicresult['logstr'] = ""
dicresult['result'] = True
_assertdict(expobj,actobj,dicresult)
print dicresult
if dicresult['result']:
asserttrue(params,"assert dict success" + dicresult['logstr'],msg) //框架自带的断言函数,可忽略
else:
assertfalse(params,"assert dict failed" + dicresult['logstr'],msg) //框架自带的断言函数,可忽略
本文转自elbertchen 51CTO博客,原文链接:http://blog.51cto.com/linkyou/751876,如需转载请自行联系原作者