Best Python code snippet using localstack_python
test_mycalc.py
Source:test_mycalc.py
...35def setup_function():36 print("å¼å§è¯»åæ°æ®...")37def teardown_function():38 print("æ°æ®è¯»åç»æ...")39def test_get_data(t='add'):40 with open(yaml_path, 'r', encoding='utf-8') as f:41 data = yaml.safe_load(f)42 value = data[t]["value"]43 ids = data[t]["id"]44 return [value, ids]45 # return [value],[ids]46#47# print(type(test_get_data()))48# print(test_get_data())49# 计ç®å æ³50class Test_mycalc_Add:51 def setup_class(self):52 self.calc = myCalc()53 self.calc = myCalc()54 print("I'm setup_class")55 def teardown_class(self):56 print("I'm teardown_class")57 def setup(self):58 print("å¼å§å æ³è®¡ç®")59 def teardown(self):60 print("ç»æå æ³è®¡ç®")61 # åæ°ç´æ¥èµå¼62 @pytest.mark.add163 @pytest.mark.parametrize(64 'a,b,expect',65 [(1, 2, 3), (0.1, 0.8, 0.9), (0, 8, 8), pytest.param(7, 8, 50, marks=pytest.mark.xfail)],66 ids=['int', 'float', 'zero', 'xfail']67 )68 def test_add(self, a, b, expect):69 calc_result = self.calc.add(a, b)70 if type(a) == float and type(b) == float:71 a = round(a, 3)72 b = round(b, 3)73 calc_result = round(calc_result, 3)74 assert calc_result == expect75 # åæ°ä»yamlä¸è¯»å76 @pytest.mark.add277 @pytest.mark.parametrize(78 'a,b,expect',79 test_get_data()[0],80 ids=test_get_data()[1]81 )82 def test_add_yaml(self, a, b, expect):83 calc_result = self.calc.add(a, b)84 assert calc_result == expect85 def test_a(self):86 result = self.calc.add(1, 2)87 assert 3 == result88 print("æµè¯ç¨ä¾test_a")89 t = map(lambda x: x + 1, range(1, 6))90 print(list(t))91# 计ç®é¤æ³92# ç¨äºä¸¤ç§æ¹æ³æè·å¼å¸¸ï¼1.pythonèªå¸¦try exceptæºå¶ 2.pytest.raisesæºå¶93class Test_mycalc_Divide:94 def setup_class(self):95 self.calc = myCalc()96 print("I'm setup class from Test_mycalc_Divide")97 def teardown_class(self):98 print("I'm teardown class from Test_mycalc_Divide")99 def setup(self):100 print("å¼å§é¤æ³è®¡ç®")101 def teardown(self):102 print("ç»æé¤æ³è®¡ç®")103 # åæ°ç´æ¥èµå¼104 @pytest.mark.divide1105 @pytest.mark.parametrize('a,b,expect', [106 (100, 2, 50),107 (0.8, 4, 0.2),108 (7, 0, 7),109 ('test', 'hello', 1)], ids=['int', 'float', 'zero', 'str'])110 def test_divide(self, a, b, expect):111 if b == 0:112 with pytest.raises(ZeroDivisionError) as exc_info: # pytest.raiseæ¯æ¥éæéè¿ãä¸æå¼å¸¸ï¼ å®å°±ä¼æ¥éäº113 calc_result = self.calc.divide(a, b)114 # raise ZeroDivisionError("division by zero")115 # if exc_info.type == ZeroDivisionError:116 # assert exc_info.type is ZeroDivisionError117 # assert exc_info.value.args[0] == "division by zero"118 elif type(a) == str or type(b) == str:119 with pytest.raises(TypeError) as exc_info:120 calc_result = self.calc.divide(a, b)121 else:122 calc_result = self.calc.divide(a, b)123 assert calc_result == expect124 # åæ°ä»yamlä¸è¯»å125 @pytest.mark.divide2126 @pytest.mark.parametrize('a,b,expect',127 test_get_data("divide")[0],128 ids=test_get_data("divide")[1])129 def test_divide_yaml(self, a, b, expect):130 try:131 if b == 0:132 raise ZeroDivisionError133 elif type(a) == str or type(b) == str:134 raise TypeError # 注æï¼ç¨pythonèªå¸¦çtry exceptæºå¶ææå¼å¸¸çæ¶åï¼å¦æå¼åå¼å¸¸ï¼tryç»æéå©ä¸ç代ç å°ä¸è½æ§è¡135 calc_result = self.calc.divide(a, b) # å¦æå¼åZeroDivisionError,TypeErrorå¼å¸¸ï¼è¿ä¸¤å¥ä¸ä¼æ§è¡ï¼ä½æ¯åé¢çexceptè¿æ¯ä¼èµ°å°136 assert calc_result == expect137 except ZeroDivisionError as e:138 print("å¼åå¼å¸¸ï¼", repr(e))139 except TypeError as e:140 print("å¼åå¼å¸¸ï¼", repr(e))141 except Exception as e:142 print("å¼åå¼å¸¸ï¼", repr(e))...
test_program.py
Source:test_program.py
1import pytest2from tests import test_get_data3import random4# pd.set_option('display.max_columns', None)5# pd.set_option('display.max_rows', None)6@pytest.fixture(autouse=True)7def set_up():8 yield9 print("\næµè¯ç»æ")10def test_object_class_avg():11 print('\næ£å¨æ§è¡æµè¯æ¨¡å-----ç级平åå')12 lst_average, class_avg, name_sheet = test_get_data.get_class_avg()13 # éæºåä¸ä¸ªè¡¨14 sheet = random.choice(range(len(lst_average)))15 # 计ç®çæ°æ®16 class_average = class_avg[sheet].dropna(axis=1, how='all')17 # excelæ°æ®18 class_avg_data = lst_average[sheet]19 class_avg_data.columns = class_average.columns20 class_avg_data = class_avg_data.applymap("{0:.03f}".format)21 class_avg_data = class_avg_data.astype(float)22 print("éæºéæ©ç页帧ï¼", name_sheet[sheet])23 print(class_average)24 print(class_avg_data)25 result = class_average == class_avg_data26 if False in result.values:27 print(result)28 print("æ°æ®ä¸ä¸è´çç´¢å¼ï¼", result[result == 'False'].index.tolist())29 assert class_average.equals(class_avg_data)30def test_object_class_ach():31 print('\næ£å¨æ§è¡æµè¯æ¨¡å-----ç级达æ度')32 lst_achieve, class_ach, name_sheet = test_get_data.get_class_avg()33 # éæºåä¸ä¸ªè¡¨34 sheet = random.choice(range(len(lst_achieve)))35 # 计ç®çæ°æ®36 class_achieve = class_ach[sheet].dropna(axis=1, how='all')37 # excelæ°æ®38 class_ach_data = lst_achieve[sheet]39 class_ach_data.columns = class_achieve.columns40 class_ach_data = class_ach_data.applymap("{0:.03f}".format)41 class_ach_data = class_ach_data.astype(float)42 print("éæºéæ©ç页帧ï¼", name_sheet[sheet])43 result = class_achieve == class_ach_data44 if False in result.values:45 print(result)46 print("æ°æ®ä¸ä¸è´çç´¢å¼ï¼", result[result == 'False'].index.tolist())47 assert class_achieve.equals(class_ach_data)48def test_target_scores():49 print('\næ£å¨æ§è¡æµè¯æ¨¡å-----åç®æ å¾ååè¾¾æ度')50 lst_scores, lst_excel_scores, name_sheet = test_get_data.get_scores()51 # éæºåä¸ä¸ªè¡¨52 sheet = random.choice(range(len(lst_scores)))53 print("éæºéæ©ç页帧ï¼", name_sheet[sheet])54 # 计ç®çåç®æ å¾å55 test_score = lst_scores[sheet]56 test_score = test_score.loc[:, (test_score != 0).any(axis=0)]57 test_score = test_score.loc[:, (test_score != 'nan').all(axis=0)]58 test_score = test_score.astype(float)59 test_score = test_score.applymap("{0:.02f}".format)60 # excelæ°æ®61 df_excel_data = lst_excel_scores[sheet].dropna(axis=0, how='all')62 df_excel_data = df_excel_data.dropna(axis=1, how='all')63 df_excel_data.columns = test_score.columns64 df_excel_data = df_excel_data.applymap("{0:.02f}".format)65 result = test_score == df_excel_data66 if False in result.values:67 print(result)68 print("æ°æ®ä¸ä¸è´çç´¢å¼ï¼", result[result == 'False'].index.tolist())69 assert test_score.equals(df_excel_data)70def test_final_achieve():71 print('\næ£å¨æ§è¡æµè¯æ¨¡å-----æç»è¾¾æ度')72 count_achieve, excel_achieve = test_get_data.get_final_achieve()73 excel_achieve = excel_achieve.applymap("{0:.03f}".format)74 excel_achieve = excel_achieve.astype(float)75 result = count_achieve == excel_achieve76 if False in result.values:77 print(result)78 print("æ°æ®ä¸ä¸è´çç´¢å¼ï¼", result[result == 'False'].index.tolist())79 assert count_achieve.equals(excel_achieve)80def test_static():81 print('\næ£å¨æ§è¡æµè¯æ¨¡å-----ç»è®¡é¨å')82 static_count_data, static_excel_data = test_get_data.static_data()83 static_count_data = static_count_data.fillna(value=0)84 static_excel_data = static_excel_data.fillna(value=0)85 result = static_count_data == static_excel_data86 if False in result.values:87 print(result)88 print("æ°æ®ä¸ä¸è´çç´¢å¼ï¼", result[result == 'False'].index.tolist())89 assert static_count_data.equals(static_excel_data)90def test_all_data():91 print('\næ£å¨æ§è¡æµè¯æ¨¡å-----è¾¾æ度æ£ç¹å¾é¡µå¸§å¯¹æ¯')92 count_data, excel_data = test_get_data.all_data_contrast()93 result = count_data == excel_data94 if False in result.values:95 print(result)96 print('æ°æ®ä¸ä¸è´çç´¢å¼ï¼', result[result == 'False'].index.tolist())97 assert count_data.equals(excel_data)98# if __name__ == '__main__':...
test_scraper.py
Source:test_scraper.py
...7"""The following tests make sure all classes are returning dictionaries. """8class AirlinersTestCase(unittest.TestCase):9 def setUp(self):10 self.scraper = Airliners()11 def test_get_data(self):12 test_dict = self.scraper.get_data()13 self.assertTrue(type(test_dict)==dict)14 def tearDown(self):15 self.scraper.housekeeping()16class AircraftbluebookTestCase(unittest.TestCase):17 def setUp(self):18 self.scraper = Aircraftbluebook()19 def test_get_data(self):20 test_dict = self.scraper.get_data()21 self.assertTrue(type(test_dict)==dict)22 def tearDown(self):23 self.scraper.housekeeping()24class ContentzoneTestCase(unittest.TestCase):25 def setUp(self):26 self.scraper = Contentzone()27 def test_get_data(self):28 test_dict = self.scraper.get_data()29 self.assertTrue(type(test_dict)==dict)30 def tearDown(self):...
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!!