Today I wrote a deep assertion method for the return value in THE HTTP interface test, in JSON format. Words do not say, directly on the code, feel good can leave a message to support me. Thank you for the initial version of selenium_Python and the many other selenium_python members.

def compare_json_data(A, B, xpath='.'): if isinstance(A, list) and isinstance(B, list): for i in range(len(A)): try: compare_json_data(A[i], B[i], xpath + '[%s]'%str(i)) except: '%(xpath, I) if isinstance(A, dict) and isinstance(B, dict): for I in A: try: B[I] except: '%(xpath, I) continue if not (isinstance(a.set (I), (list, dict)) or isinstance(b.set (I)) (list, dict))): if type(A.get(i)) ! = type(b.et (I)): print 'print' % % s/s for excellent for A is % s, B is % s' % (xpath, I, type (al-qeada et (I)), type (B.g et (I))) elif al-qeada et (I)! = b.et (I): print 'print' in [A] % s/has excellent for A is % s, % s is % s' % B (xpath, I, atul gawande et (I), B.g et (I)) continue compare_json_data (al-qeada et (I), B.g et (I), xpath + '/' + str(i)) return if type(A) ! = type(B): print 'elif A! Elif A! Elif A! Elif A! Elif A! = B and type(A) is not list: print ' A is % S, B is %s' % (xpath, A, B) # A = {' b ':,2,5,8 [1],' c ': 3,' d ': 2,' f ': [1, 2, 3],' g ': [1, 2, 3, [2,' 2 ', 2]], 'h', '5', 'I' : None, 'j' : False, "k" : {" l ": {' m: [{' n ': 12}]}}} B = {' B ': [1, 2,' 3 ', 'c' : 2, 'e', '4', 'f' :,2,3,5 [1], and 'g' : [1, 2, 3, [1, 2]], 'h' : [1, 2], 'I' : None, 'j' : True, 'k' : {' l ': {' m: [{2}' n ':]}}} compare_json_data(A,B)Copy the code

For example, if you find a parameter type that is not the same, you don’t need to continue to run it. If you can’t find it, you can directly declare failure or manually trigger an exception. The assertion failed, and two values need to be returned to determine the final use case performance through self.assert (assert method, etc.). Throw an exception and change print to assert. Below is the result of the above execution. Test data: A = {‘ b ‘:,2,5,8 [1], “c” : 3, “d” : 2, “f” : [1, 2, 3],’ g ‘: [1, 2, 3, [2,’ 2 ‘, 2]], ‘h’, ‘5’, ‘I’ : None, “j” : False, “k” : {” l “: {” m” : [{12} ‘n’ :]}}} B = {‘ B ‘: [1, 2, 3]’ ‘, ‘c’ : 2, ‘e’, ‘4’, ‘f’ :,2,3,5 [1], and ‘g’ : [1, 2, 3, [1, 2]], ‘h’ : [1, 2], ‘I’ : None, “j” : True, “k” : {” l “: {” m” : [{2} ‘n’ :]}}}

Output result:

Finally, welcome to leave a message. I thought I was on a solo.