Best Python code snippet using pyresttest_python
data.py
Source: data.py
...86 logging.debug(f"match_stdout_to_json_file: stdout={stdout!r}")87 logging.debug(f"match_stdout_to_json_file: file={obj!r}")88 if isinstance(obj, dict):89 stdout = {key: value for key, value in stdout.items() if key in obj}90 assert_dict_eq(obj, stdout)91 elif isinstance(obj, list):92 obj = {"key": obj}93 stdout = {"key": stdout}94 assert_dict_eq(obj, stdout)95 assert_dict_eq(obj, stdout)96 else:97 assert_eq(obj, stdout)98def match_stdout_to_json_file_exactly(ctx, filename=None):99 runcmd_get_stdout = globals()["runcmd_get_stdout"]100 assert_eq = globals()["assert_eq"]101 assert_dict_eq = globals()["assert_dict_eq"]102 stdout = runcmd_get_stdout(ctx)103 stdout = json.loads(stdout.strip())104 obj = json.load(open(filename))105 logging.debug(f"match_stdout_to_json_file: stdout={stdout!r}")106 logging.debug(f"match_stdout_to_json_file: file={obj!r}")107 if isinstance(obj, list):108 obj = {"key": obj}109 stdout = {"key": stdout}110 assert_dict_eq(obj, stdout)111 elif isinstance(obj, dict):112 assert_dict_eq(obj, stdout)113 else:114 assert_eq(obj, stdout)115def manifests_match(ctx, expected=None, actual=None):116 assert_eq = globals()["assert_eq"]117 assert_dict_eq = globals()["assert_dict_eq"]118 logging.debug(f"comparing manifests {expected} and {actual}")119 expected_objs = list(yaml.safe_load_all(open(expected)))120 actual_objs = list(yaml.safe_load_all(open(actual)))121 logging.debug(f"there are {len(expected_objs)} and {len(actual_objs)} objects")122 i = 0123 while expected_objs and actual_objs:124 e = expected_objs.pop(0)125 a = actual_objs.pop(0)126 logging.debug(f"comparing manifest objects at index {i}:")127 logging.debug(f" expected: {e}")128 logging.debug(f" actual : {a}")129 assert_dict_eq(e, a)130 i += 1131 logging.debug(f"remaining expected objecvts: {expected_objs}")132 logging.debug(f"remaining actual objecvts : {actual_objs}")133 assert_eq(expected_objs, [])134 assert_eq(actual_objs, [])135 logging.debug(f"manifests {expected} and {actual} match")136def file_is_readable_by_owner(ctx, filename=None):137 assert_eq = globals()["assert_eq"]138 st = os.lstat(filename)139 mode = stat.S_IMODE(st.st_mode)140 logging.debug("file mode: %o", mode)141 assert_eq(mode, 0o400)142def file_does_not_contain(ctx, filename=None, pattern=None):143 data = open(filename).read()...
kmeans_tests.py
Source: kmeans_tests.py
2 assign_data_to_closest_centroid, update_assignment,\3 mean_of_points, update_centroids4import numpy as np5# help functions6def assert_dict_eq(dict1, dict2):7 assert type(dict1) is dict8 assert type(dict2) is dict9 # keys10 assert dict1.keys() == dict2.keys()11 # values12 for k, v in dict2.items():13 matrix2 = np.array(v)14 matrix1 = np.array(dict1[k])15 assert np.allclose(np.sort(matrix1, axis=0), np.sort(matrix2, axis=0))16def setup_data_centroids():17 data = [18 [-1.01714716, 0.95954521, 1.20493919, 0.34804443],19 [-1.36639346, -0.38664658, -1.02232584, -1.05902604],20 [1.13659605, -2.47109085, -0.83996912, -0.24579457],21 [-1.48090019, -1.47491857, -0.6221167, 1.79055006],22 [-0.31237952, 0.73762417, 0.39042814, -1.1308523],23 [-0.83095884, -1.73002213, -0.01361636, -0.32652741],24 [-0.78645408, 1.98342914, 0.31944446, -0.41656898],25 [-1.06190687, 0.34481172, -0.70359847, -0.27828666],26 [-2.01157677, 2.93965872, 0.32334723, -0.1659333],27 [-0.56669023, -0.06943413, 1.46053764, 0.01723844]28 ]29 random_centroids = {30 "centroid1": [0.1839742, -0.45809263, -1.91311585, -1.48341843],31 "centroid2": [-0.71767545, 1.2309971, -1.00348728, -0.38204247],32 }33 bad_centroids = {34 "centroid1": [0.1839742, -0.45809263, -1.91311585, -1.48341843],35 "centroid2": [10, 10, 10, 10],36 }37 return data, random_centroids, bad_centroids38# tests begin39def test_eucliean_distance():40 # int41 data1 = [0, 0, 0, 0]42 data2 = [1, 1, 1, 1]43 assert euclidean_distance_between_data(data1, data2) == 244 # random45 data1 = np.random.randn(100)46 data2 = np.random.randn(100)47 assert np.allclose(np.array(euclidean_distance_between_data(data1.tolist(),48 data2.tolist())),49 np.linalg.norm(data1 - data2).tolist())50 print("test_eucliean_distance passed.")51def test_assign_data():52 # set up53 data_empty = [0, 0, 0, 0]54 data_random = [1.1, 5.3, 55, -12.1]55 centroids = {"centroid1": [1, 1, 1, 1],56 "centroid2": [-10.1, 1, 23.2, 5.099]}57 assert assign_data_to_closest_centroid(data_empty, centroids) \58 == "centroid1"59 assert assign_data_to_closest_centroid(data_random, centroids) \60 == "centroid2"61 print("test_assign_data passed.")62def test_update_assignment():63 # set up64 data, random_centroids, bad_centroids = setup_data_centroids()65 # random66 rtn = update_assignment(data, random_centroids)67 answer = {68 "centroid1": [[-1.36639346, -0.38664658, -1.02232584, -1.05902604],69 [1.13659605, -2.47109085, -0.83996912, -0.24579457],70 [-0.83095884, -1.73002213, -0.01361636, -0.3265274]],71 "centroid2": [[-1.01714716, 0.95954521, 1.20493919, 0.34804443],72 [-1.48090019, -1.47491857, -0.6221167, 1.79055006],73 [-0.31237952, 0.73762417, 0.39042814, -1.1308523],74 [-0.78645408, 1.98342914, 0.31944446, -0.41656898],75 [-1.06190687, 0.34481172, -0.70359847, -0.27828666],76 [-2.01157677, 2.93965872, 0.32334723, -0.1659333],77 [-0.56669023, -0.06943413, 1.46053764, 0.01723844]]78 }79 assert_dict_eq(rtn, answer)80 # bad81 rtn = update_assignment(data, bad_centroids)82 answer = {83 "centroid1": [[-1.36639346, -0.38664658, -1.02232584, -1.05902604],84 [1.13659605, -2.47109085, -0.83996912, -0.24579457],85 [-0.83095884, -1.73002213, -0.01361636, -0.3265274],86 [-1.01714716, 0.95954521, 1.20493919, 0.34804443],87 [-1.48090019, -1.47491857, -0.6221167, 1.79055006],88 [-0.31237952, 0.73762417, 0.39042814, -1.1308523],89 [-0.78645408, 1.98342914, 0.31944446, -0.41656898],90 [-1.06190687, 0.34481172, -0.70359847, -0.27828666],91 [-2.01157677, 2.93965872, 0.32334723, -0.1659333],92 [-0.56669023, -0.06943413, 1.46053764, 0.01723844]]93 }94 assert_dict_eq(rtn, answer)95 print("test_update_assignment passed.")96def test_mean_of_points():97 # empty98 data = [99 [0, 0, 0, 0],100 [0, 0, 0, 0],101 ]102 assert mean_of_points(data) == [0, 0, 0, 0]103 # random104 data = np.random.randn(10, 4)105 assert np.allclose(np.array(mean_of_points(data.tolist())),106 data.mean(axis=0))107 print("test_mean_of_points passed.")108def test_update_centroids():109 # set up110 data, random_centroids, bad_centroids = setup_data_centroids()111 # random112 assignment_dict = update_assignment(data, random_centroids)113 answer = {114 'centroid2': [-1.03386497, 0.774388037, 0.33899735, 0.023455955],115 'centroid1': [-0.35358541, -1.529253186, -0.62530377, -0.543782673]116 }117 rtn = update_centroids(assignment_dict)118 assert_dict_eq(rtn, answer)119 # bad120 assignment_dict = update_assignment(data, bad_centroids)121 answer = {122 'centroid1': [-0.82978110, 0.08329567, 0.04970701, -0.146715632]123 }124 rtn = update_centroids(assignment_dict)125 assert_dict_eq(rtn, answer)126 print("test_update_centroids passed.")127if __name__ == '__main__':128 test_eucliean_distance()129 test_assign_data()130 test_update_assignment()131 test_mean_of_points()132 test_update_centroids()...
Check out the latest blogs from LambdaTest on this topic:
Recently, I was going through some of the design patterns in Java by reading the book Head First Design Patterns by Eric Freeman, Elisabeth Robson, Bert Bates, and Kathy Sierra.
In addition to the four values, the Agile Manifesto contains twelve principles that are used as guides for all methodologies included under the Agile movement, such as XP, Scrum, and Kanban.
Most test automation tools just do test execution automation. Without test design involved in the whole test automation process, the test cases remain ad hoc and detect only simple bugs. This solution is just automation without real testing. In addition, test execution automation is very inefficient.
In general, software testers have a challenging job. Software testing is frequently the final significant activity undertaken prior to actually delivering a product. Since the terms “software” and “late” are nearly synonymous, it is the testers that frequently catch the ire of the whole business as they try to test the software at the end. It is the testers who are under pressure to finish faster and deem the product “release candidate” before they have had enough opportunity to be comfortable. To make matters worse, if bugs are discovered in the product after it has been released, everyone looks to the testers and says, “Why didn’t you spot those bugs?” The testers did not cause the bugs, but they must bear some of the guilt for the bugs that were disclosed.
The purpose of developing test cases is to ensure the application functions as expected for the customer. Test cases provide basic application documentation for every function, feature, and integrated connection. Test case development often detects defects in the design or missing requirements early in the development process. Additionally, well-written test cases provide internal documentation for all application processing. Test case development is an important part of determining software quality and keeping defects away from customers.
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!!