Best Python code snippet using slash
test_parallel.py
Source:test_parallel.py
...127 parallel_suite[0].add_decorator('slash.skipped("reason")')128 parallel_suite[0].expect_skip()129 results = parallel_suite.run().session.results130 assert results.is_success(allow_skips=True)131 assert results.get_num_skipped() == 1132 for result in results:133 if result.is_skip():134 assert 'reason' in result.get_skips()135def test_session_warnings(parallel_suite):136 parallel_suite[0].append_line("import warnings")137 parallel_suite[0].append_line("warnings.warn('message')")138 session_results = parallel_suite.run().session139 assert len(session_results.warnings) == 1140 [w] = session_results.warnings141 assert w.message == 'message'142def test_traceback_vars(parallel_suite):143 #code to be inserted:144 # def test_traceback_frames():145 # num = 0146 # a()147 #148 # def a():149 # x=1150 # assert False151 parallel_suite[0].append_line("def a():")152 parallel_suite[0].append_line(" num = 0")153 parallel_suite[0].append_line(" b()")154 parallel_suite[0].append_line("def b():")155 parallel_suite[0].append_line(" x=1")156 parallel_suite[0].append_line(" assert False")157 parallel_suite[0].append_line("a()")158 parallel_suite[0].expect_failure()159 summary = parallel_suite.run(num_workers=1)160 results = summary.session.results161 found_failure = 0162 for result in results:163 if result.is_failure():164 found_failure += 1165 assert len(result.get_failures()) == 1166 assert len(result.get_failures()[0].traceback.frames) == 3167 assert found_failure == 1168def test_result_data_not_picklable(parallel_suite):169 parallel_suite[0].append_line("import socket")170 parallel_suite[0].append_line("s = socket.socket()")171 parallel_suite[0].append_line("slash.context.result.data.setdefault('socket', s)")172 summary = parallel_suite.run()173 [result] = summary.get_all_results_for_test(parallel_suite[0])174 assert result.data == {}175def test_result_data_is_picklable(parallel_suite):176 parallel_suite[0].append_line("slash.context.result.data.setdefault('num', 1)")177 summary = parallel_suite.run()178 [result] = summary.get_all_results_for_test(parallel_suite[0])179 assert 'num' in result.data180 assert result.data['num'] == 1181def test_result_details_not_picklable(parallel_suite):182 parallel_suite[0].append_line("import socket")183 parallel_suite[0].append_line("s = socket.socket()")184 parallel_suite[0].append_line("slash.context.result.details.append('socket', s)")185 summary = parallel_suite.run()186 [result] = summary.get_all_results_for_test(parallel_suite[0])187 assert result.details.all() == {}188def test_result_details_is_picklable(parallel_suite):189 parallel_suite[0].append_line("slash.context.result.details.append('num', 1)")190 summary = parallel_suite.run()191 [result] = summary.get_all_results_for_test(parallel_suite[0])192 details = result.details.all()193 assert 'num' in details194 assert details['num'] == [1]195def test_parameters(parallel_suite):196 parallel_suite[0].add_parameter(num_values=1)197 summary = parallel_suite.run()198 assert summary.session.results.is_success()199def test_requirements(parallel_suite):200 parallel_suite[0].add_decorator('slash.requires(False)')201 parallel_suite[0].expect_skip()202 results = parallel_suite.run().session.results203 assert results.get_num_skipped() == 1204 assert results.get_num_successful() == len(parallel_suite) - 1205 assert results.is_success(allow_skips=True)206def test_is_test_code(parallel_suite):207 parallel_suite[0].when_run.error()208 summary = parallel_suite.run()209 [result] = summary.get_all_results_for_test(parallel_suite[0])210 [err] = result.get_errors()211 assert err.traceback.frames[-1].is_in_test_code()212 error_json = err.traceback.to_list()213 assert error_json[-1]['is_in_test_code']214def test_parallel_resume(parallel_suite):215 parallel_suite[0].when_run.fail()216 result = parallel_suite.run()217 resumed = get_tests_to_resume(result.session.id)...
test_result.py
Source:test_result.py
...32def test_get_num_skips_no_not_run(suite, suite_test):33 suite_test.add_decorator('slash.requires(False)')34 suite_test.expect_skip()35 results = suite.run().session.results36 assert results.get_num_skipped(include_not_run=False) == 037 assert results.get_num_skipped(include_not_run=True) == 138 assert results.get_num_skipped() == 139def test_result_summary(suite):40 suite[2].when_run.fail()41 suite[3].when_run.raise_exception()42 suite[4].when_run.raise_exception()43 suite[5].when_run.skip()44 results = suite.run().session.results45 assert results.get_num_errors() == 246 assert results.get_num_failures() == 147 assert results.get_num_skipped() == 148 assert results.get_num_successful() == len(suite) - 449 assert results.get_num_not_run() == 050def test_result_not_run(suite, suite_test, is_last_test):51 suite_test.when_run.fail()52 for test in suite.iter_all_after(suite_test, assert_has_more=not is_last_test):53 test.expect_not_run()54 summary = suite.run(additional_args=['-x'])55 num_not_run = summary.session.results.get_num_not_run()56 if is_last_test:57 assert num_not_run == 058 else:59 assert 0 < num_not_run < len(suite)60def test_result_not_run_zero_when_all_success(suite):61 summary = suite.run()62 assert summary.session.results.get_num_not_run() == 063def test_has_errors_or_failures(suite):64 suite[2].when_run.fail()65 suite[3].when_run.raise_exception()66 results = suite.run().session.results67 assert not results[0].has_errors_or_failures()68 assert results[2].has_errors_or_failures()69 assert results[3].has_errors_or_failures()70def test_has_skips(suite):71 suite[1].when_run.skip()72 results = suite.run().session.results73 assert not results[0].has_skips()74 assert results[1].has_skips()75def test_result_data_is_unique():76 class SampleTest(slash.Test):77 def test_1(self):78 pass79 def test_2(self):80 pass81 session = run_tests_assert_success(SampleTest)82 [result1, result2] = session.results83 assert result1.data is not result2.data84def test_result_test_garbage_collected(gc_marker):85 class SomeTest(slash.Test):86 def test_something(self):87 pass88 # we have to run another test at the end to make sure Slash's internal _last_test89 # doesn't refer to our test90 class OtherTest(slash.Test):91 def test_something(self):92 pass93 marks = []94 runnable_tests = []95 test_funcs = [SomeTest, OtherTest]96 @slash.hooks.register97 def tests_loaded(tests): # pylint: disable=unused-variable98 runnable_tests.extend(tests)99 marks.extend(list(gc_marker.mark(t) for t in runnable_tests[:-1]))100 with slash.Session() as s: # pylint: disable=unused-variable101 session = run_tests_assert_success(test_funcs) # pylint: disable=unused-variable102 del runnable_tests[:]103 gc.collect()104 for mark in marks:105 assert mark.destroyed106def test_add_error_traceback_for_manually_added_errors(suite, suite_test):107 suite_test.append_line('slash.add_error("msg")')108 suite_test.expect_error()109 [result] = suite.run().get_all_results_for_test(suite_test)110 [err] = result.get_errors()111 assert err.traceback112def test_is_global_result(suite, suite_test):113 suite_test.append_line('assert not slash.context.result.is_global_result()')114 result = suite.run()115 assert result.session.results.global_result.is_global_result()116def test_global_result_is_success(suite, suite_test):117 suite_test.when_run.fail()118 assert not suite.run().session.results.global_result.is_success()119def test_global_result_error_without_started_context():120 with slash.Session() as session:121 with handling_exceptions(swallow=True):122 1/0 # pylint: disable=pointless-statement123 assert not session.results.is_success()124def test_session_cleanups_under_global_result(suite, suite_test):125 @suite_test.append_body126 def __code__(): # pylint: disable=unused-variable127 def cleanup():128 slash.context.result.data['ok'] = True129 slash.add_cleanup(cleanup, scope='session')130 res = suite.run()131 assert res.session.results.global_result.data['ok']132@pytest.mark.parametrize('log_path', [None, 'a/b/c'])133@pytest.mark.parametrize('log_subpath', [None, 'my_errors.log'])134def test_log_paths(log_path, log_subpath, config_override, logs_dir):135 config_path = 'log.highlights_subpath'136 extra_logs = ['/my/extra/log_{}'.format(i) for i in range(2)]137 config_override(config_path, log_subpath)138 with slash.Session() as curr_session:139 result = curr_session.results.global_result140 result.set_log_path(log_path)141 expected_logs = [log_path] if log_path else []142 if log_subpath:143 expected_logs.append(logs_dir.join('files').join(log_subpath))144 assert result.get_log_path() is log_path145 assert result.get_log_paths() == expected_logs146 for extra_log in extra_logs:147 result.add_extra_log_path(extra_log)148 assert result.get_log_paths() == expected_logs + extra_logs149@pytest.mark.parametrize('error_adder', (Result.add_error, Result.add_failure))150def test_result_not_started_with_errors(error_adder):151 result = Result()152 assert not result.is_started()153 assert result.is_not_run()154 error_adder(result, "error")155 assert not result.is_started()156 assert not result.is_not_run()157class SessionResultTest(TestCase):158 def setUp(self):159 super(SessionResultTest, self).setUp()160 self.results = [161 Result() for _ in range(10)162 ]163 for r in self.results:164 r.mark_started()165 # one result with both errors and failures166 try:167 1 / 0168 except:169 self.results[1].add_error()170 self.results[1].add_failure()171 # one result with failure172 self.results[2].add_failure()173 # one result with error174 self.results[3].add_error()175 self.results[5].add_error()176 # one result will skip177 self.results[4].add_skip("Reason")178 # and one result will skip with error179 self.results[5].add_skip("Reason")180 num_finished = 7181 for result in self.results[:num_finished]:182 result.mark_finished()183 self.result = SessionResults(Session())184 for index, r in enumerate(self.results):185 self.result._results_dict[index] = r # pylint: disable=protected-access186 def test_counts(self):187 self.assertEqual(self.result.get_num_results(), len(self.results))188 self.assertEqual(self.result.get_num_successful(), 2)189 # errors take precedence over failures190 self.assertEqual(self.result.get_num_errors(), 3)191 self.assertEqual(self.result.get_num_skipped(), 2)...
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!!