Best Python code snippet using slash
test_logging.py
Source:test_logging.py
...138################################################################################139## Fixtures140@pytest.fixture141def session():142 session = run_tests_assert_success(SampleTest)143 return session144@pytest.fixture145def errors_log_path(request, config_override, tmpdir, logs_dir):146 subpath = 'subdir/errors.log'147 config_override('log.highlights_subpath', subpath)148 return logs_dir.join('files').join(subpath)149@pytest.fixture150def links_dir(logs_dir):151 return logs_dir.join("links")152@pytest.fixture153def files_dir(logs_dir):154 return logs_dir.join("files")155_TOKEN = "logging-test"156_SESSION_START_MARK = "session-start-mark"157_SESSION_END_MARK = "session-end-mark"158_silenced_logger = logbook.Logger("silenced_logger")159################################################################################160## Legacy Tests161class LogFormattingTest(TestCase):162 def setUp(self):163 super(LogFormattingTest, self).setUp()164 self.log_path = self.get_new_path()165 self.override_config(166 "log.root", self.log_path167 )168 self.override_config(169 "log.format", "-- {record.message} --"170 )171 self.override_config("log.subpath", "debug.log")172 def test(self):173 self.session = run_tests_assert_success(SampleTest)174 with open(os.path.join(self.log_path, "debug.log")) as logfile:175 for line in logfile:176 self.assertTrue(line.startswith("-- "))177 self.assertTrue(line.endswith(" --\n"))178class LoggingTest(TestCase):179 def test(self):180 self.log_path = self.get_new_path()181 self.override_config(182 "log.root",183 self.log_path,184 )185 self.override_config(186 "log.subpath",187 os.path.join("{context.session.id}",188 "{context.test.__slash__.test_index0:03}-{context.test_id}", "debug.log")189 )190 self.override_config(191 "log.session_subpath",192 os.path.join("{context.session.id}", "debug.log")193 )194 self.override_config(195 "log.silence_loggers",196 [_silenced_logger.name]197 )198 self.addCleanup(gossip.unregister_token, _TOKEN)199 slash.hooks.session_start.register( # pylint: disable=no-member200 functools.partial(_mark, _SESSION_START_MARK), token=_TOKEN)201 slash.hooks.session_end.register( # pylint: disable=no-member202 functools.partial(_mark, _SESSION_END_MARK), token=_TOKEN)203 self.addCleanup(gossip.unregister_token, _TOKEN)204 self.session = run_tests_assert_success(SampleTest)205 self.tests_metadata = [206 result.test_metadata for result in self.session.results.iter_test_results()]207 self._test_all_run()208 self._test_test_logs_written()209 self._test_session_logs()210 self._test_no_silenced_logger_records()211 def _test_all_run(self):212 methods = [213 method_name for method_name in dir(SampleTest)214 if method_name.startswith("test")215 ]216 self.assertTrue(methods)217 self.assertEqual(len(self.tests_metadata), len(methods))218 def _test_test_logs_written(self):...
test_result.py
Source:test_result.py
...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()...
test_test_parameters.py
Source:test_test_parameters.py
...16def test_parameters_toggle():17 @slash.parameters.toggle('param')18 def test_example(param):19 _set('param', param)20 session = run_tests_assert_success(test_example)21 assert [False, True] == sorted(result.data['param'] for result in session.results)22def test_dict_parameter_values():23 values = [{'value': 1}, {'value': 2}]24 @slash.parametrize('param', copy.deepcopy(values))25 def test_example(param):26 _set('param', param)27 session = run_tests_assert_success(test_example)28 assert values == sorted((result.data['param'] for result in session.results), key=values.index)29def test_duplicate_parameters():30 values = ["a", "b", "a"]31 @slash.parametrize('param', copy.deepcopy(values))32 def test_example(param):33 _set('param', param)34 session = run_tests_assert_success(test_example)35 assert sorted(values) == sorted(result.data['param'] for result in session.results)36def test_before_after_parameters(cartesian):37 class Parameterized(slash.Test):38 @slash.parameters.iterate(a=cartesian.before_a.make_set())39 def before(self, a): # pylint: disable=arguments-differ40 _set("before_a", a)41 @slash.parameters.iterate(b=cartesian.b.make_set(), c=cartesian.c.make_set())42 def test(self, b, c):43 _set("b", b)44 _set("c", c)45 @slash.parameters.iterate(d=cartesian.after_d.make_set())46 def after(self, d): # pylint: disable=arguments-differ47 _set("after_d", d)48 session = run_tests_assert_success(Parameterized)49 assert len(session.results) == len(cartesian)50 cartesian.check(result.data for result in session.results)51@pytest.mark.parametrize('with_override', [True, False])52def test_before_parameters_inheritence(cartesian, with_override):53 class BaseTest(slash.Test):54 @slash.parameters.iterate(a=cartesian.before_1_a.make_set())55 def before(self, a): # pylint: disable=arguments-differ56 _set("before_1_a", a)57 class DerivedTest(BaseTest):58 @slash.parameters.iterate(a=cartesian.before_2_a.make_set(), b=cartesian.before_2_b.make_set())59 def before(self, a, b): # pylint: disable=arguments-differ60 if with_override:61 super(DerivedTest, self).before(a=a)62 else:63 super(DerivedTest, self).before()64 _set("before_2_a", a)65 _set("before_2_b", b)66 def test(self):67 pass68 session = run_tests_assert_success(DerivedTest)69 assert len(session.results) == len(cartesian)70 if with_override:71 cartesian.assign_all(72 source_name='before_2_a', target_name='before_1_a')73 cartesian.check(result.data for result in session.results)74def test_parametrization_tuples():75 @slash.parametrize(('a', 'b'), [(1, 2), (11, 22)])76 @slash.parametrize('c', [3, 33])77 def test_something(a, b, c):78 _set("params", (a, b, c))79 session = run_tests_assert_success(test_something)80 results = [result.data['params'] for result in session.results.iter_test_results()]81 expected = set([82 (1, 2, 3), (1, 2, 33), (11, 22, 3), (11, 22, 33)83 ])84 assert len(expected) == len(results)85 assert expected == set(results)86def test_parametrization_tuples_invalid_length():87 with pytest.raises(RuntimeError) as caught:88 # pylint: disable=unused-argument, unused-variable89 @slash.parametrize(('a', 'b'), [(1, 2), (1,), (11, 22)])90 def test_something(a, b, c):91 pass92 assert 'Invalid parametrization value' in str(caught.value)93 assert 'invalid length' in str(caught.value)...
__init__.py
Source:__init__.py
...79 for err in itertools.chain(result.get_errors(), result.get_failures(), result.get_skips()):80 _logger.debug("Unsuccessful result: {0}", err)81 return session82run_tests_in_session.__test__ = False83def run_tests_assert_success(test_class_path_or_iterator, session=None):84 session = run_tests_in_session(test_class_path_or_iterator, session=session)85 assert session.results.is_success(), "Run did not succeed"86 return session87run_tests_assert_success.__test__ = False88def make_runnable_tests(thing):89 return slash.loader.Loader().get_runnables(thing)90def resolve_and_run(thing):91 slash.context.session.fixture_store.resolve()92 with slash.context.session.get_started_context():93 tests = make_runnable_tests(thing)94 slash.runner.run_tests(tests)95 return list(slash.context.session.results.iter_test_results())96def without_pyc(filename):97 if filename.endswith('.pyc'):...
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!!