Best Python code snippet using Testify_python
assertions.py
Source:assertions.py
...12try:13 STRING_TYPE = basestring14except: # py3k15 STRING_TYPE = str16def _val_subtract(val1, val2, dict_subtractor, list_subtractor):17 """18 Find the difference between two container types19 Returns:20 The difference between the values as defined by list_subtractor() and21 dict_subtractor() if both values are the same container type.22 None if val1 == val223 val1 if type(val1) != type(val1)24 Otherwise - the difference between the values25 """26 if val1 == val2:27 # if the values are the same, return a degenerate type28 # this case is not used by list_subtract or dict_subtract29 return type(val1)()30 if isinstance(val1, dict) and isinstance(val2, dict):31 val_diff = dict_subtractor(val1, val2)32 elif isinstance(val1, (list, tuple)) and isinstance(val2, (list, tuple)):33 val_diff = list_subtractor(val1, val2)34 else:35 val_diff = val136 return val_diff37def _dict_subtract(dict1, dict2):38 """39 Return key,value pairs from dict1 that are not in dict240 Returns:41 A new dict 'res_dict' with the following properties:42 For all (key, val) pairs where key appears in dict2:43 if dict1[val] == dict2[val] then res_dict[val] is not defined44 else res_dict[val] == dict1[val]45 If vals are themselves dictionaries the algorim is applied recursively.46 Example:47 _dict_subtract({48 1: 'one',49 2: 'two',50 3: {'a': 'A', 'b': 'B'},51 4: {'c': 'C', 'd': 'D'}52 },53 {54 2: 'two',55 3: {'a': 'A', 'b': 'B'},56 4: {'d': 'D'},57 5: {'e': 'E'}58 }) => {1: 'one', 4: {'c': 'C'}}59 """60 # make a result we can edit61 result = dict(dict1)62 # find the common keys -- i.e., the ones we might need to subtract63 common_keys = set(dict1.keys()) & set(dict2.keys())64 for key in common_keys:65 val1, val2 = dict1[key], dict2[key]66 if val1 == val2:67 # values are the same: subtract68 del result[key]69 else:70 # values are different: set the output key to the different between the values71 result[key] = _val_subtract(val1, val2, _dict_subtract, _list_subtract)72 return result73def _list_subtract(list1, list2):74 """75 Returns the difference between list1 and list2.76 _list_subtract([1,2,3], [3,2,1]) == [1,3]77 If any items in the list are container types, the method recursively calls78 itself or _dict_subtract() to subtract the child79 containers.80 """81 # call val_subtract on all items that are not the same82 res_list = [_val_subtract(val1, val2, _dict_subtract, _list_subtract)83 for val1, val2 in zip(list1, list2) if val1 != val2]84 # now append items that come after any item in list185 res_list += list1[len(list2):]86 # return a tuple of list1 is a tuple87 if isinstance(list1, tuple):88 return tuple(res_list)89 else:90 return res_list91def assert_raises(*args, **kwargs):92 """Assert an exception is raised as a context manager or by passing in a93 callable and its arguments.94 As a context manager:95 >>> with assert_raises(Exception):96 ... raise Exception...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!