Best Python code snippet using molecule_python
tests.py
Source:tests.py
...5class StepError(Exception):6 def __init__(self, step, msg):7 self.step = step8 super().__init__(msg)9def execute_scenario(scenario, game, cross_user, circle_user, ensure_valid_scenario=True):10 steps = parser.parse_steps(scenario, ensure_valid=ensure_valid_scenario)11 for step in steps:12 if step['symbol'] == ttt.CROSS_SYMBOL:13 user = cross_user14 else:15 user = circle_user16 try:17 core.update_game(18 game_id=game.id,19 user=user,20 board_nr=step['board_nr'],21 position=step['position']22 )23 except core.MoveError as error:24 raise StepError(step, msg=str(error))25class TTTTest(TestCase):26 def setUp(self):27 self.u1 = User.objects.create_user("u1", "u1@example.com", "haslo123")28 self.u2 = User.objects.create_user("u2", "u2@example.com", "haslo123")29 self.session = core.invite(self.u1, self.u2)30 self.game = core.create_game(self.session)31 def test_invalid_move_on_filled_mini_board(self):32 scenario = '''33 ...|...|...*...|...|...*...|...|...34 ...|X05|...*...|...|...*...|...|...35 ...|...|...*...|...|...*...|...|...36 *** *** *** *** *** *** *** *** ***37 ...|...|...*O04|O08|...*...|...|...38 ...|X07|...*O06|X01|...*...|...|...39 ...|...|...*O02|...|...*...|...|...40 *** *** *** *** *** *** *** *** ***41 ...|...|...*...|...|...*...|...|...42 ...|X03|...*...|...|...*...|...|...43 ...|...|...*...|...|...*...|...|...44 '''45 try:46 execute_scenario(scenario, self.game, cross_user=self.u1, circle_user=self.u2)47 except StepError as error:48 self.assertEquals(error.step['code'], 'O08')49 self.assertEquals('Wrong mini-board, board nr: 5 is filled', str(error))50 def test_allowed_any_mini_board_when_suggested_mini_board_is_filled(self):51 scenario = '''52 ...|...|...*...|...|...*...|...|...53 ...|X05|...*...|...|...*...|...|...54 ...|...|...*...|...|...*...|...|...55 *** *** *** *** *** *** *** *** ***56 ...|...|...*O04|...|...*...|...|...57 ...|X07|...*O06|X01|...*...|...|...58 ...|...|...*O02|...|...*...|...|...59 *** *** *** *** *** *** *** *** ***60 ...|...|...*...|...|...*...|...|...61 ...|X03|...*...|...|...*...|...|...62 ...|...|...*...|...|...*...|...|O0863 '''64 execute_scenario(scenario, self.game, cross_user=self.u1, circle_user=self.u2)65 last_move = core.get_game_moves(self.game)[-1]66 self.assertEquals(last_move.board_nr, 9)67 self.assertEquals(last_move.position, 9)68 self.assertEquals(last_move.value, ttt.CIRCLE_SYMBOL)69 def test_win_player_second(self):70 scenario = '''71 X11|O24|...*X21|...|...*...|...|X0572 ...|O12|X15*...|...|...*...|...|...73 ...|O22|X01*...|...|...*O06|...|X0974 *** *** *** *** *** *** *** *** ***75 ...|...|...*O14|O20|O04*...|...|...76 ...|X19|...*...|X13|...*...|...|O1677 ...|...|...*...|...|...*...|...|X1778 *** *** *** *** *** *** *** *** ***79 ...|...|...*X23|...|...*O10|...|O0880 ...|X03|...*...|...|...*O18|...|...81 ...|...|X07*...|...|...*O02|...|...82 '''83 execute_scenario(scenario, self.game, cross_user=self.u1, circle_user=self.u2)84 updated_game = Game.objects.get(id=self.game.id)85 self.assertEquals(updated_game.result, ttt.CIRCLE_SYMBOL)86 def test_win_player_first(self):87 scenario = '''88 O20|...|...*...|...|...*X13|...|O1289 ...|O14|...*...|...|...*...|X17|...90 O18|...|O02*...|...|...*...|...|X0591 *** *** *** *** *** *** *** *** ***92 ...|...|X11*X01|...|...*...|...|O1693 ...|...|...*...|X15|...*...|...|...94 O22|...|...*...|...|X07*...|...|X0995 *** *** *** *** *** *** *** *** ***96 X19|...|O04*...|...|...*...|...|...97 ...|X21|...*...|...|...*O10|O06|O0898 ...|...|X23*...|...|...*X03|...|...99 '''100 execute_scenario(scenario, self.game, cross_user=self.u1, circle_user=self.u2)101 updated_game = Game.objects.get(id=self.game.id)102 self.assertEquals(updated_game.result, ttt.CROSS_SYMBOL)103 def test_draw(self):104 scenario = '''105 O38|...|X37*...|X11|...*O34|...|X33106 ...|O20|X35*...|X17|...*O36|...|...107 ...|...|O24*O12|X05|...*O22|...|X27108 *** *** *** *** *** *** *** *** ***109 ...|...|...*...|...|...*...|O16|...110 O30|O14|O08*X07|X21|X15*...|...|...111 ...|...|...*...|...|O18*X29|X09|X03112 *** *** *** *** *** *** *** *** ***113 X23|...|...*...|O10|...*X19|O04|O26114 X13|...|O02*...|O06|...*...|X31|O28115 X01|...|...*...|O32|...*...|...|X25116 '''117 try:118 execute_scenario(scenario, self.game, cross_user=self.u1, circle_user=self.u2, ensure_valid_scenario=True)119 except StepError as error:120 print("Wykryto bÅÄ
d", error.step)121 print("message", str(error))122 else:123 updated_game = Game.objects.get(id=self.game.id)124 print("wynik koÅcowy:", updated_game.result)125# skoÅczyÄ126 def test_wrong_move(self):127 scenario = '''128 ...|...|...*...|...|...*...|...|...129 ...|...|...*...|O02|...*...|...|...130 ...|...|...*...|...|...*...|...|...131 *** *** *** *** *** *** *** *** ***132 ...|...|...*...|...|...*...|...|...133 ...|X01|...*...|...|...*...|...|...134 ...|...|...*...|...|...*...|...|...135 *** *** *** *** *** *** *** *** ***136 ...|...|...*...|...|...*...|...|...137 ...|...|...*...|...|...*...|...|...138 ...|...|...*...|...|...*...|...|...139 '''140 try:141 execute_scenario(scenario, self.game, cross_user=self.u1, circle_user=self.u2, ensure_valid_scenario=False)142 except StepError as error:143 self.assertEquals(str(error), "Wrong mini-board, expected: 5, got: 2")144 def test_two_moves_in_row(self):145 scenario = '''146 ...|...|...*...|...|...*...|...|...147 ...|...|...*...|...|...*...|...|...148 ...|...|...*...|...|...*...|...|...149 *** *** *** *** *** *** *** *** ***150 ...|...|...*...|...|...*...|...|...151 ...|...|...*...|X02|...*...|X01|...152 ...|...|...*...|...|...*...|...|...153 *** *** *** *** *** *** *** *** ***154 ...|...|...*...|...|...*...|...|...155 ...|...|...*...|...|...*...|...|...156 ...|...|...*...|...|...*...|...|...157 '''158 try:159 execute_scenario(scenario, self.game, cross_user=self.u1, circle_user=self.u2, ensure_valid_scenario=False)160 except StepError as error:161 self.assertEquals(str(error), "Required the next symbol, got: X")162 def test_circle_first_move(self):163 scenario = '''164 ...|...|...*...|...|...*...|...|...165 ...|...|...*...|...|...*...|...|...166 ...|...|...*...|...|...*...|...|...167 *** *** *** *** *** *** *** *** ***168 ...|...|...*...|...|...*...|...|...169 ...|...|...*...|...|...*...|O01|...170 ...|...|...*...|...|...*...|...|...171 *** *** *** *** *** *** *** *** ***172 ...|...|...*...|...|...*...|...|...173 ...|...|...*...|...|...*...|...|...174 ...|...|...*...|...|...*...|...|...175 '''176 try:177 execute_scenario(scenario, self.game, cross_user=self.u1, circle_user=self.u2, ensure_valid_scenario=False)178 except StepError as error:179 self.assertEquals(str(error), "Required cross symbol")...
test_pandas_cv.py
Source:test_pandas_cv.py
...11 RandomizedSearchCV,12 GridSearchCV13)14from tests import utils15def execute_scenario(model_type, is_cl, with_prep, cv_type, holdout_type):16 cv = _get_cv(model_type, is_cl, with_prep, cv_type)17 df_training = utils.get_input_df(100, with_prep)18 df_validation = utils.get_input_df(100, with_prep)19 df_test = utils.get_input_df(10, with_prep)20 target_column = "target_cl" if is_cl else "target_rg"21 feature_columns = ["column{}".format(i) for i in range(6)]22 model = cv.fit_cv_pandas(df_training, target_column, feature_columns, n_fold=3) \23 if holdout_type == "cv" \24 else cv.fit_holdout_pandas(df_training, target_column, feature_columns, ratio_training=0.8) \25 if holdout_type == "holdout_ratio" \26 else cv.fit_holdout_pandas(df_training, target_column, feature_columns, df_validation)27 _assert_prediction(model, df_test, is_cl)28def test_random_linear_holdout_ratio_cl():29 execute_scenario("linear", True, False, "random", "holdout_ratio")30def test_random_tree_holdout_2dfs_rg():31 execute_scenario("tree", False, False, "random", "holdout_2dfs")32def test_random_tree_with_prep_cv_cl():33 execute_scenario("tree", True, True, "random", "cv")34def test_grid_linear_holdout_ratio_cl():35 execute_scenario("linear", True, False, "grid", "holdout_ratio")36def test_grid_tree_with_prep_holdout_2dfs_rg():37 execute_scenario("tree", False, True, "grid", "holdout_2dfs")38def test_grid_tree_cv_cl():39 execute_scenario("tree", True, False, "grid", "cv")40def _get_cv(model_type, is_cl, with_prep, cv_type):41 estimator = _get_estimator(model_type, is_cl, with_prep)42 metric = "roc_auc" if is_cl else "neg_root_mean_squared_error"43 if cv_type == "random":44 params = _get_params_random(model_type, is_cl, with_prep)45 return RandomizedSearchCV(estimator, params, scoring=metric)46 else:47 params = _get_params_grid(model_type, is_cl, with_prep)48 return GridSearchCV(estimator, params, scoring=metric)49def _get_estimator(model_type, is_cl, with_preprocessing):50 if model_type == "linear":51 ml_estimator = linear_model.LogisticRegression(solver="liblinear") if is_cl else linear_model.Lasso()52 else:53 ml_estimator = tree.DecisionTreeClassifier() if is_cl else tree.DecisionTreeRegressor()...
test.py
Source:test.py
...10class test_selenium(unittest.TestCase):11 def setUp(self):12 self.driver = drivers.getDriver()13 def test_show_page(self):14 execute_scenario(self.driver, self.show_page)15 def show_page(self):16 driver = self.driver17 basic.show_page(driver)18 header = driver.find_element_by_xpath('//*[@id="root"]')19 self.assertIsNotNone(header)20 def test_show_logout_button(self):21 execute_scenario(self.driver, self.show_logout_button)22 def show_logout_button(self):23 driver = self.driver24 basic.show_page(driver)25 is_exist_logout_button = is_missing_element(driver,26 '//*[@id="menu-appbar"]/div[3]/ul/li')27 self.assertFalse(is_exist_logout_button, "Logout Button is not exist")28 basic.click_account_icon(driver)29 logoutButton = driver.find_element_by_xpath(30 '//*[@id="menu-appbar"]/div[3]/ul/li')31 self.assertTrue(logoutButton.is_enabled())32 def tearDown(self):33 self.driver.close()34def execute_scenario(driver, fn):35 try:36 fn()37 except Exception as e:38 print("Exception in test: ", e)39 driver.get_screenshot_as_file('screens/ss-%s.png' % fn.__name__)40 raise e41def is_missing_element(driver, xpath):42 try:43 driver.find_element_by_xpath(xpath)44 return True45 except selenium.common.exceptions.NoSuchElementException as err:46 return False47 else:48 print(arg, 'has', len(f.readlines()), 'lines')...
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!!