接口自动化,断言方法,深度定位错误

本文介绍了一种用于接口自动化的断言方法,该方法通过深度比较两个JSON对象(包括list和dict),并精确地定位到不同之处的具体路径,从而帮助开发者高效地发现和解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接口自动化,断言方法,深度定位错误。

 

代码如下:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 # @Time    : 2017-07-27 13:49
 4 
 5 # 断言方法,比较两个list或dict的不同之处
 6 
 7 a= {'b':[1,2,5,8],'c':3,'d':2,'f':[1,2,3],'g':[1,2,3,[2,'2',2]],'h':'5'}
 8 b= {'b':[1,2,'3'],'c':2,'e':'4','f':[1,2,3,5],'g':[1,2,3,[1,2]],'h':[1,2]}
 9 
10 def compare_json_data(A, B, L = [], xpath = '.'):
11     if isinstance(A, list) and isinstance(B, list):
12         for i in range(len(A)):
13             try:
14                 compare_json_data(A[i], B[i], L, xpath + '[%s]' % str(i))
15             except:
16                 L.append('▇▇▇ A中的key %s[%s]未在B中找到\n' % (xpath, i))
17     if isinstance(A, dict) and isinstance(B, dict):
18         for i in A:
19             try:
20                 B[i]
21             except:
22                 L.append('▇▇▇ A中的key %s/%s 未在B中找到\n' % (xpath, i))
23                 continue
24             if not (isinstance(A.get(i), (list, dict)) or isinstance(B.get(i), (list, dict))):
25                 if type(A.get(i)) != type(B.get(i)):
26                     L.append('▇▇▇ 类型不同参数在[A]中的绝对路径:  %s/%s  ►►► A is %s, B is %s \n' % (xpath, i, type(A.get(i)), type(B.get(i))))
27                 elif A.get(i) != B.get(i):
28                     L.append('▇▇▇ 仅内容不同参数在[A]中的绝对路径:  %s/%s  ►►► A is %s, B is %s \n' % (xpath, i, A.get(i), B.get(i)))
29                 continue
30             compare_json_data(A.get(i), B.get(i), L, xpath + '/' + str(i))
31         return
32     if type(A) != type(B):
33         L.append('▇▇▇ 类型不同参数在[A]中的绝对路径:  %s  ►►► A is %s, B is %s \n' % (xpath, type(A), type(B)))
34     elif A != B and type(A) is not list:
35         L.append('▇▇▇ 仅内容不同参数在[A]中的绝对路径:  %s  ►►► A is %s, B is %s \n' % (xpath, A, B))
36     return L
37 
38 def Assert(A,B):
39     C = []
40     compare_json_data(A, B, C)
41     assert len(C) == 0, "\n"+"".join(C)
42 Assert(a, b)

效果图如下:

转载于:https://www.cnblogs.com/hxjxb/p/7245826.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值