Best Python code snippet using autotest_python
heap_tests.py
Source:heap_tests.py
1from heap import *2length = 83values = [18, 6, 7, -3, 44, 10, 6]4heap = MinHeap(length)5num_tests_passed = 06num_tests_failed = 07num_tests = 08print("====================================")9print("PART 1")10print("Initializing with values", values)11print("====================================")12print("====================================")13print("Test Case 1")14print("Correct Initialization")15print("====================================")16num_tests += 117if heap.get_heap_array() == [None, None, None, None, None, None, None, None]:18 print("TEST PASSED")19 num_tests_passed += 120else:21 print("TEST FAILED")22 num_tests_failed += 123print()24print("====================================")25print("Test Case 2")26print("Correct Insertion")27print("====================================")28num_tests += 129for value in values:30 heap.insert(value)31if heap.get_heap_array() == [-3, 6, 6, 18, 44, 10, 7, None]:32 print("TEST PASSED")33 num_tests_passed += 134else:35 print("TEST FAILED")36 num_tests_failed += 137print()38print("====================================")39print("Test Case 3")40print("Correct Extraction")41print("====================================")42num_tests += 143if heap.extract_min() == -3:44 if heap.get_heap_array() == [6, 7, 6, 18, 44, 10, None, None]:45 print("TEST PASSED")46 num_tests_passed += 147 else:48 print("TEST FAILED: Incorrect array output")49 num_tests_failed += 150else:51 print("TEST FAILED: Incorrect value output")52 num_tests_failed += 153print()54length = 855values = [3, 3, 8, 6, 4, 2, 8, 1]56print("====================================")57print("PART 2")58print("Initializing with values", values)59print("====================================")60heap = MinHeap(length)61print("====================================")62print("Test Case 4")63print("Correct Initialization")64print("====================================")65num_tests += 166if heap.get_heap_array() == [None, None, None, None, None, None, None, None]:67 print("TEST PASSED")68 num_tests_passed += 169else:70 print("TEST FAILED")71 num_tests_failed += 172print()73print("====================================")74print("Test Case 5")75print("Correct Insertion")76print("====================================")77num_tests += 178for value in values:79 heap.insert(value)80if heap.get_heap_array() == [1, 2, 3, 3, 4, 8, 8, 6]:81 print("TEST PASSED")82 num_tests_passed += 183else:84 print("TEST FAILED")85 num_tests_failed += 186print()87print("====================================")88print("Test Case 6")89print("Correct Extraction")90print("====================================")91num_tests += 192if heap.extract_min() == 1:93 if heap.get_heap_array() == [2, 3, 3, 6, 4, 8, 8, None]:94 print("TEST PASSED")95 num_tests_passed += 196 else:97 print("TEST FAILED: Incorrect array output")98 num_tests_failed += 199else:100 print("TEST FAILED: Incorrect value output")101 num_tests_failed += 1102print()103length = 8104values = [None, None, None, None, None, None, None, None]105print("====================================")106print("PART 3")107print("Initializing with values", values)108print("====================================")109heap = MinHeap(length)110print("====================================")111print("Test Case 7")112print("Correct Initialization")113print("====================================")114num_tests += 1115if heap.get_heap_array() == [None, None, None, None, None, None, None, None]:116 print("TEST PASSED")117 num_tests_passed += 1118else:119 print("TEST FAILED")120 num_tests_failed += 1121print()122print("====================================")123print("Test Case 8")124print("Correct Insertion")125print("====================================")126num_tests += 1127for value in values:128 heap.insert(value)129if heap.get_heap_array() == [None, None, None, None, None, None, None, None]:130 print("TEST PASSED")131 num_tests_passed += 1132else:133 print("TEST FAILED")134 num_tests_failed += 1135print()136print("====================================")137print("Test Case 9")138print("Correct Extraction")139print("====================================")140num_tests += 1141if heap.extract_min() == None:142 if heap.get_heap_array() == values:143 print("TEST PASSED")144 num_tests_passed += 1145 else:146 print("TEST FAILED: Incorrect array output")147 num_tests_failed += 1148else:149 print("TEST FAILED: Incorrect value output")150 num_tests_failed += 1151print()152length = 8153values = [3, 3, 8, 6, 4, 2, 8, 1, 9]154print("====================================")155print("PART 4")156print("Initializing with values", values)157print("====================================")158heap = MinHeap(length)159print("====================================")160print("Test Case 10")161print("Correct Initialization")162print("====================================")163num_tests += 1164if heap.get_heap_array() == [None, None, None, None, None, None, None, None]:165 print("TEST PASSED")166 num_tests_passed += 1167else:168 print("TEST FAILED")169 num_tests_failed += 1170print()171print("====================================")172print("Test Case 11")173print("Correct Insertion")174print("====================================")175num_tests += 1176for value in values:177 heap.insert(value)178if heap.get_heap_array() == [1, 2, 3, 3, 4, 8, 8, 6]:179 print("TEST PASSED")180 num_tests_passed += 1181else:182 print("TEST FAILED")183 num_tests_failed += 1184print()185print("====================================")186print("Test Case 12")187print("Correct Extraction")188print("====================================")189num_tests += 1190if heap.extract_min() == 1:191 if heap.get_heap_array() == [2, 3, 3, 6, 4, 8, 8, None]:192 print("TEST PASSED")193 num_tests_passed += 1194 else:195 print("TEST FAILED: Incorrect array output")196 num_tests_failed += 1197else:198 print("TEST FAILED: Incorrect value output")199 num_tests_failed += 1200print()201length = 0202values = [3, 3, 8, 6, 4, 2, 8, 1, 9]203print("====================================")204print("PART 5")205print("Initializing with values", values)206print("====================================")207heap = MinHeap(length)208print("====================================")209print("Test Case 13")210print("Correct Initialization")211print("====================================")212num_tests += 1213if heap.get_heap_array() == []:214 print("TEST PASSED")215 num_tests_passed += 1216else:217 print("TEST FAILED")218 num_tests_failed += 1219print()220print("====================================")221print("Test Case 14")222print("Correct Insertion")223print("====================================")224num_tests += 1225for value in values:226 heap.insert(value)227if heap.get_heap_array() == []:228 print("TEST PASSED")229 num_tests_passed += 1230else:231 print("TEST FAILED")232 num_tests_failed += 1233print()234print("====================================")235print("Test Case 15")236print("Correct Extraction")237print("====================================")238num_tests += 1239if heap.extract_min() == None:240 if heap.get_heap_array() == []:241 print("TEST PASSED")242 num_tests_passed += 1243 else:244 print("TEST FAILED: Incorrect array output")245 num_tests_failed += 1246else:247 print("TEST FAILED: Incorrect value output")248 num_tests_failed += 1249print()250length = 100251values = [3, 3, 8, 6, 4, 2, 8, 1, 9]252print("====================================")253print("PART 6")254print("Initializing with values", values)255print("====================================")256heap = MinHeap(length)257print("====================================")258print("Test Case 16")259print("Correct Initialization")260print("====================================")261num_tests += 1262if heap.get_heap_array() == [None] * length:263 print("TEST PASSED")264 num_tests_passed += 1265else:266 print("TEST FAILED")267 num_tests_failed += 1268print()269print("====================================")270print("Test Case 17")271print("Correct Insertion")272print("====================================")273num_tests += 1274for value in values:275 heap.insert(value)276test_array = [1, 2, 3, 3, 4, 8, 8, 6, 9]277for i in range(length - len(test_array)):278 test_array.append(None)279if heap.get_heap_array() == test_array:280 print("TEST PASSED")281 num_tests_passed += 1282else:283 print("TEST FAILED")284 print("Array Returned:")285 print(heap.get_heap_array())286 num_tests_failed += 1287 print()288 print("Correct Array:")289 print(test_array)290print()291print("====================================")292print("Test Case 18")293print("Correct Extraction")294print("====================================")295num_tests += 1296test_array = [2, 3, 3, 6, 4, 8, 8, 9, None]297for i in range(length - len(test_array)):298 test_array.append(None)299if heap.extract_min() == 1:300 if heap.get_heap_array() == test_array:301 print("TEST PASSED")302 num_tests_passed += 1303 else:304 print("TEST FAILED: Incorrect array output")305 print("Your array:")306 print(heap.get_heap_array())307 print("Correct array:")308 print(test_array)309 num_tests_failed += 1310 num_tests_failed += 1311else:312 print("TEST FAILED: Incorrect value output")313print()314print("====================================")315print("SUMMARY")316print("====================================")317print("Total Tests:", num_tests)318print("Total Tests Passed: {}/{}".format(num_tests_passed, num_tests))...
views.py
Source:views.py
1from django.conf import settings2from django.db import transaction3from django.http import HttpResponse4from django.views.generic import View5import functools6import json7import inspect8import importlib9import time10from . import SmokeTest, ApplicationTestResultSet11def test_class(cls):12 o = cls()13 return o.run()14def run_single_class_test(name, obj):15 if not issubclass(obj, SmokeTest):16 return (0, 0, 0, 0, 0, [], [])17 if name == "SmokeTest":18 # skip the parent class, which is usually imported19 return (0, 0, 0, 0, 0, [], [])20 (run, passed, failed, errored, f_tests, e_tests) = test_class(obj)21 return (1, run, passed, failed, errored, f_tests, e_tests)22def test_application(app):23 """ should return an ApplicationTestResultSet """24 num_test_classes = 025 num_tests_run = 026 num_tests_passed = 027 num_tests_failed = 028 num_tests_errored = 029 failed_tests = []30 errored_tests = []31 a = None32 try:33 a = importlib.import_module("%s.smoke" % app)34 except ImportError:35 # no 'smokes' module for the app36 pass37 except Exception as e:38 num_tests_errored += 139 errored_tests.append(40 'Exception while importing smoke test script: %s' % e)41 if a is not None:42 e_tests = []43 try:44 for name, obj in inspect.getmembers(a, inspect.isclass):45 (classes, run, passed, failed, errored,46 f_tests, e_tests) = run_single_class_test(name, obj)47 num_test_classes += classes48 num_tests_run += run49 num_tests_passed += passed50 num_tests_failed += failed51 num_tests_errored += errored52 failed_tests = failed_tests + f_tests53 errored_tests = errored_tests + e_tests54 except Exception as e:55 # probably an error in setUp() or tearDown()56 num_tests_errored += 157 e_tests.append('Exception during test: %s' % e)58 finally:59 errored_tests = errored_tests + e_tests60 return ApplicationTestResultSet(61 num_test_classes, num_tests_run,62 num_tests_passed, num_tests_errored,63 num_tests_failed, failed_tests,64 errored_tests)65def make_failed_report(result_sets):66 return "\n\n".join(67 [f for f in functools.reduce(68 lambda x, y: x + y, [r.failed for r in result_sets])])69def make_errored_report(result_sets):70 return "\n\n".join(71 [f for f in functools.reduce(72 lambda x, y: x + y, [r.errored for r in result_sets])])73def plaintext_output(status, num_test_classes, num_tests_run,74 num_tests_passed, num_tests_failed,75 num_tests_errored, finish, start, failed_report,76 errored_report):77 return """%s78test classes: %d79tests run: %d80tests passed: %d81tests failed: %d82tests errored: %d83time: %fms84%s85%s""" % (status, num_test_classes, num_tests_run, num_tests_passed,86 num_tests_failed, num_tests_errored, (finish - start) * 1000,87 failed_report, errored_report)88def skip_apps():89 if hasattr(settings, 'SMOKETEST_SKIP_APPS'):90 return settings.SMOKETEST_SKIP_APPS91 return []92class IndexView(View):93 @transaction.atomic()94 def get(self, request):95 sp1 = transaction.savepoint()96 try:97 start = time.time()98 skip = skip_apps()99 result_sets = [test_application(app)100 for app in settings.INSTALLED_APPS101 if app not in skip]102 finish = time.time()103 all_passed = functools.reduce(104 lambda x, y: x & y, [r.passed() for r in result_sets])105 num_test_classes = sum([r.num_test_classes for r in result_sets])106 num_tests_run = sum([r.num_tests_run for r in result_sets])107 num_tests_passed = sum([r.num_tests_passed for r in result_sets])108 num_tests_failed = sum([r.num_tests_failed for r in result_sets])109 num_tests_errored = sum([r.num_tests_errored for r in result_sets])110 failed_report = make_failed_report(result_sets)111 errored_report = make_errored_report(result_sets)112 http_status = 500113 if all_passed:114 status = "PASS"115 http_status = 200116 else:117 status = "FAIL"118 response = HttpResponse(119 status=http_status,120 content=plaintext_output(121 status, num_test_classes, num_tests_run,122 num_tests_passed, num_tests_failed,123 num_tests_errored, finish, start,124 failed_report, errored_report),125 content_type="text/plain")126 if ('HTTP_ACCEPT' in request.META and (127 'application/json' in request.META['HTTP_ACCEPT'])):128 response = HttpResponse(129 json.dumps(130 dict(131 status=status,132 test_classes=num_test_classes,133 tests_run=num_tests_run,134 tests_passed=num_tests_passed,135 tests_failed=num_tests_failed,136 tests_errored=num_tests_errored,137 failed_tests=functools.reduce(138 lambda x, y: x + y,139 [r.failed for r in result_sets]),140 errored_tests=functools.reduce(141 lambda x, y: x + y,142 [r.errored for r in result_sets]),143 time=(finish - start) * 1000,144 )),145 content_type="application/json",146 )147 finally:148 # always roll back the smoketest view149 transaction.savepoint_rollback(sp1)...
selection_sort.py
Source:selection_sort.py
1"""2--- SELECTION SORT --- 3The intuition for the algorithm is as follows.4We can divide the list of numbers into a sorted and unsorted part5The sorted part can be incrementally built by finding the smallest number in the unsorted part6and placing it at the tail of the sorted part7I think it is easiest though to build up intuition with an example... 8Let's imagine our list of numbers is L = [3, 4, 2, 1]9Note the list can be named anything but I chose L for List10We imagine the sorted portion of the list is S (S for sorted) which starts empty11and the unsorted part is the entire L12So our initial state is13L = [3, 4, 2, 1]14S = []15S will not actually be another list, but will denote the sorted part of L that we build16I think it's just useful to see to keep track of how much we've sorted17We go through L from position 1 to 4 and find the smallest number 1 at position 418We swap this number 1 with the number at postion 1 of the list, 3, which gives us19L = [1, 4, 2, 3]20S = [1]21Now L is sorted from position 1 to 122So we go from postion 2 to 4 and find the smallest number 2 and swap it with the number at position 223This gives us24L = [1, 2, 4, 3]25S = [1, 2]26Now L is sorted from position 1 to 227So we go from postion 3 to 4 and find the smallest number 3 and swap it with the number at position 328This gives us29L = [1, 2, 3, 4]30S = [1, 2, 3]31Since L is sorted from position 1 to 3 and only one number remains, L must be sorted32as we can see above.33L = [1, 2, 3, 4]34S = [1, 2, 3, 4]35So the algorithm can be stated as 36Given a list of numbers L371. If L is empty, return L immediately 382. Otherwise set p = 1 (p for position) and set N = the length of L393. If p = N, we are finished so return L404. Otherwise find the smallest number in L from p to N415. swap that number with the number at p426. Set p = p + 1437. Go back to step 344Let's see what this looks like in python45Note that the first position of a list in python is given by 0 not 146"""47def selection_sort(list_of_numbers):48 # Set N = length of list 49 N = len(list_of_numbers)50 if N == 0:51 return []52 # set p = 053 p = 054 55 # while we still have numbers in list_of_numbers56 # note we need -1 here because python starts at 0 not 1 so last position in the list is N-157 while p < N - 1:58 # find the smallest number in the remaining unsorted part59 # start by assuming it's the number at position p60 smallest_number = list_of_numbers[p]61 smallest_position = p62 63 # then check the rest of the list64 for i in range(p+1, N):65 if list_of_numbers[i] < smallest_number:66 smallest_position = i67 smallest_number = list_of_numbers[i]68 # swap the number at position p with the smallest number69 # we use temp so we don't lose a number70 temp_number = list_of_numbers[smallest_position]71 list_of_numbers[smallest_position] = list_of_numbers[p]72 list_of_numbers[p] = temp_number73 # increase p by one 74 p = p+175 # the loop has finished so we return list_of_numbers76 return list_of_numbers77"""78---- TEST OUR SORT FUNCTION ----79We will create a function that tries some simple test lists 80Note here that we use += 1 to increment by 181"""82def selection_sort_test():83 84 print(" --- Testing selection sort --- ")85 # keep track of how many tests are successful and unssuccesful 86 num_tests_passed = 087 num_tests_failed = 088 # random order89 test_list = [6,1,8,2,9,4,3,0,5,7] 90 if selection_sort(test_list) == [0,1,2,3,4,5,6,7,8,9]:91 num_tests_passed = num_tests_passed + 192 else:93 print("Test failed for:", test_list)94 num_tests_failed = num_tests_failed + 195 # empty list96 test_list = [] 97 if selection_sort(test_list) == []:98 num_tests_passed = num_tests_passed + 199 else: 100 print("Test failed for:", test_list)101 num_tests_failed = num_tests_failed + 1102 # positive sorted list103 test_list = [1,2,3,4] 104 if selection_sort(test_list) == [1,2,3,4]: 105 num_tests_passed = num_tests_passed + 1106 else:107 print("Test failed for:", test_list)108 num_tests_failed = num_tests_failed + 1109 # only negative110 test_list = [-1,-3,-2,-4] 111 if selection_sort(test_list) == [-4,-3,-2,-1]:112 num_tests_passed = num_tests_passed + 1113 else:114 print("Test failed for:", test_list)115 num_tests_failed = num_tests_failed + 1116 # positive and negative117 test_list = [-1, 1, -2, 2, -3, 3, -4, 4] 118 if selection_sort(test_list) == [-4,-3,-2,-1,1,2,3,4]:119 num_tests_passed = num_tests_passed + 1120 else: 121 print("Test failed for:", test_list) 122 num_tests_failed = num_tests_failed + 1123 # including decimal numbers124 test_list = [1, 0.1, -1] 125 if selection_sort(test_list) == [-1, 0.1, 1]:126 num_tests_passed = num_tests_passed + 1127 else: 128 print("Test failed for:", test_list)129 num_tests_failed = num_tests_failed + 1130 131 print("Number of tests passed:", num_tests_passed)132 print("Number of tests failed:", num_tests_failed)133# --- RUN THE TESTING CODE---134selection_sort_test()...
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!!