Best Python code snippet using slash
test_requirements.py
Source:test_requirements.py
...22 def test_something():23 pass24 with slash.Session() as session:25 with session.get_started_context():26 slash.runner.run_tests(make_runnable_tests(test_something))27 [result] = [28 res for res in session.results.iter_all_results() if not res.is_global_result()29 ]30 assert result.is_skip()31 assert checkpoint1.called32 assert checkpoint2.called33def test_requirements_raises_exception(suite, suite_test):34 @suite_test.file.append_body35 def __code__(): # pylint: disable=unused-variable36 def fail_predicate(): # pylint: disable=unused-variable37 raise Exception("Failing")38 suite_test.add_decorator("slash.requires(fail_predicate)")39 suite_test.expect_error()40 summary = suite.run()41 assert not summary.session.results.is_success(allow_skips=True)42def test_requirements_mismatch_session_success(suite, suite_test):43 suite_test.add_decorator("slash.requires(False)")44 suite_test.expect_skip()45 summary = suite.run()46 assert summary.session.results.is_success(allow_skips=True)47@pytest.mark.parametrize("requirement_fullfilled", [True, False])48@pytest.mark.parametrize("use_message", [True, False])49@pytest.mark.parametrize("use_fixtures", [True, False])50@pytest.mark.parametrize("message_in_retval", [True, False])51def test_requirements(52 suite,53 suite_test,54 requirement_fullfilled,55 use_fixtures,56 use_message,57 message_in_retval,58):59 message = "requires something very important"60 if use_message and message_in_retval:61 retval = "({}, {!r})".format(requirement_fullfilled, message)62 else:63 retval = requirement_fullfilled64 suite_test.add_decorator(65 "slash.requires((lambda: {}), {!r})".format(66 retval, message if use_message and not message_in_retval else ""67 )68 )69 if not requirement_fullfilled:70 suite_test.expect_skip()71 if use_fixtures:72 suite_test.depend_on_fixture(suite.slashconf.add_fixture())73 results = suite.run()74 if requirement_fullfilled:75 assert results[suite_test].is_success()76 else:77 assert not results[suite_test].is_started()78 assert results[suite_test].is_skip()79 if use_message:80 [skip] = results[suite_test].get_skips()81 assert message in skip82def test_requirements_functions_no_message(suite, suite_test):83 suite_test.add_decorator(_UNMET_REQ_DECORATOR)84 suite_test.expect_skip()85 results = suite.run()86 result = results[suite_test]87 [skip] = result.get_skips()88 assert "lambda" in skip89def test_requirements_on_class():90 def req1():91 pass92 def req2():93 pass94 @slash.requires(req1)95 class Test(slash.Test):96 @slash.requires(req2)97 def test_something(self):98 pass99 with slash.Session():100 [test] = make_runnable_tests(Test) # pylint: disable=unbalanced-tuple-unpacking101 assert set([r._req for r in test.get_requirements()]) == set( # pylint: disable=protected-access102 [req1, req2]103 )104@pytest.fixture105def filename_test_fixture(tmpdir):106 returned = str(tmpdir.join("testfile.py"))107 with open(returned, "w") as f:108 with ExitStack() as stack:109 code = CodeFormatter(f)110 code.writeln("import slash")111 code.writeln("@slash.fixture")112 code.writeln(113 "@slash.requires({}, {})".format(_UNMET_REQ_DECORATOR, '"msg1"')114 )115 code.writeln("def fixture():")116 with code.indented():117 code.writeln("return 1")118 code.writeln("@slash.fixture(autouse=True)")119 code.writeln("@slash.requires({}, {})".format(_MET_REQ_DECORATOR, '"msg2"'))120 code.writeln("def fixture1():")121 with code.indented():122 code.writeln("return 1")123 code.writeln("class Test(slash.Test):")124 stack.enter_context(code.indented())125 code.write("def test_1(")126 code.write("self, ")127 code.writeln("fixture):")128 with code.indented():129 code.writeln("pass")130 return returned131def test_requirements_on_class_with_fixture_and_autouse_fixture(filename_test_fixture):132 with slash.Session():133 [test] = make_runnable_tests( # pylint: disable=unbalanced-tuple-unpacking134 filename_test_fixture135 )136 assert sorted([str(r) for r in test.get_requirements()]) == ["msg1", "msg2"]137def test_unmet_requirements_trigger_avoided_test_hook(suite, suite_test):138 suite_test.add_decorator(_UNMET_REQ_DECORATOR)139 suite_test.expect_skip()140 @gossip.register("slash.test_avoided")141 def test_avoided(reason): # pylint: disable=unused-variable142 slash.context.result.data["avoided"] = {143 "reason": reason,144 "test_name": slash.context.test.__slash__.address,145 }146 summary = suite.run()147 avoided_result = summary[suite_test]148 for r in summary.session.results.iter_all_results():149 if r is avoided_result:150 assert "avoided" in r.data151 assert "lambda" in r.data["avoided"]["reason"]152 assert "unmet requirement" in r.data["avoided"]["reason"].lower()153 assert r.data["avoided"]["test_name"].split("_")[-1] == suite_test.id154 else:155 assert "avoided" not in r.data156def test_adding_requirement_objects():157 class MyRequirement(slash.core.requirements.Requirement):158 pass159 req = MyRequirement("bla")160 @slash.requires(req)161 def test_something():162 pass163 with slash.Session():164 [test] = make_runnable_tests( # pylint: disable=unbalanced-tuple-unpacking165 test_something166 ) # pylint: disable=unbalanced-tuple-unpacking167 reqs = test.get_requirements()168 assert len(reqs) == 1 and reqs[0] is req169def test_cannot_specify_message_with_requirement_object():170 class MyRequirement(slash.core.requirements.Requirement):171 pass172 with pytest.raises(AssertionError) as caught:173 slash.requires(MyRequirement(""), "message")174 assert "specify message" in str(caught.value)175@pytest.mark.parametrize("is_fixture_requirement_unmet", [True, False])176def test_fixture_and_test_requirements(suite, suite_test, is_fixture_requirement_unmet):177 suite_test.depend_on_fixture(suite.slashconf.add_fixture())178 if is_fixture_requirement_unmet:179 suite_test._fixtures[0][1].add_decorator( # pylint: disable=protected-access180 _UNMET_REQ_DECORATOR181 )182 suite_test.add_decorator(_MET_REQ_DECORATOR)183 else:184 suite_test._fixtures[0][1].add_decorator( # pylint: disable=protected-access185 _MET_REQ_DECORATOR186 )187 suite_test.add_decorator(_UNMET_REQ_DECORATOR)188 suite_test.expect_skip()189 results = suite.run()190 assert results[suite_test].is_skip()191 result = results[suite_test]192 [skip] = result.get_skips()193 assert "lambda" in skip194def test_fixture_of_fixture_requirement(suite, suite_test):195 suite_test.add_decorator(_UNMET_REQ_DECORATOR)196 suite_test.depend_on_fixture(suite.slashconf.add_fixture())197 suite_test._fixtures[0][1].add_decorator( # pylint: disable=protected-access198 _MET_REQ_DECORATOR199 )200 suite_test.expect_skip()201 results = suite.run()202 assert results[suite_test].is_skip()203 result = results[suite_test]204 [skip] = result.get_skips()205 assert "lambda" in skip206def test_autouse_fixture_requirement():207 suite = Suite()208 for _ in range(5):209 test = suite.add_test(type="function")210 test.expect_skip()211 fixture = suite.get_last_file().add_fixture(autouse=True)212 fixture.add_decorator(_UNMET_REQ_DECORATOR)213 suite.run()214def test_class_requirements_siblings(suite_builder):215 @suite_builder.first_file.add_code216 def __code__(): # pylint: disable=unused-variable217 import slash # pylint: disable=redefined-outer-name, reimported218 @slash.requires(lambda: True)219 class BaseTest(slash.Test):220 pass221 @slash.requires(lambda: False) # pylint: disable=unused-variable222 class FirstTest(BaseTest):223 def test(self):224 pass225 class SecondTest(BaseTest): # pylint: disable=unused-variable226 def test(self):227 pass228 suite_builder.build().run().assert_results_breakdown(skipped=1, success=1)229@pytest.mark.parametrize("class_can_run", [True, False])230@pytest.mark.parametrize("method_can_run", [True, False])231def test_class_requirements_class_and_method(class_can_run, method_can_run):232 @slash.requires(lambda: class_can_run)233 class Test(slash.Test):234 @slash.requires(lambda: method_can_run)235 def test(self):236 pass237 with slash.Session() as session:238 with session.get_started_context():239 slash.runner.run_tests(make_runnable_tests(Test))240 results = [res for res in session.results.iter_test_results()]241 assert len(results) == 1242 if class_can_run and method_can_run:243 assert results[0].is_success()244 else:245 assert results[0].is_skip()246def test_attach_requirements_through_wraps_on_function():247 def decorator(func):248 @wraps(func)249 def new_func():250 pass251 return new_func252 req1, req2, req3, req4 = requirements = [object() for _ in range(4)]253 @slash.requires(req4)...
test_variation_info.py
Source:test_variation_info.py
...38 def fixture():39 pass40 s.fixture_store.resolve()41 with s.get_started_context():42 slash.runner.run_tests(make_runnable_tests(test_something))43 assert s.results.is_success(allow_skips=False)44 assert checkpoint.called45def test_parametrization_info_values_include_nested_fixture_values():46 value1 = str(uuid4())47 value2 = str(uuid4())48 @gossip.register('slash.test_start')49 def test_start_hook():50 slash.context.result.data['variation_values'] = slash.context.test.__slash__.variation.values.copy()51 @slash.parametrize('param', ['some_value'])52 def test_something(param, some_fixture):53 pass54 with slash.Session() as s:55 @s.fixture_store.add_fixture56 @slash.fixture57 @slash.parametrize('value', [value1, value2])58 def some_fixture(value):59 pass60 s.fixture_store.resolve()61 with s.get_started_context():62 slash.runner.run_tests(make_runnable_tests(test_something))63 assert s.results.is_success(allow_skips=False)64 all_values = []65 for result in s.results.iter_test_results():66 values = result.data['variation_values']67 all_values.append(values['some_fixture.value'])68 assert len(all_values) == 269 assert set(all_values) == {value1, value2}70def test_variation_identification():71 value1 = str(uuid4())72 value2 = str(uuid4())73 @gossip.register('slash.test_start')74 def test_start_hook():75 variation = slash.context.test.__slash__.variation76 slash.context.result.data['variation_info'] = {77 'id': variation.id.copy(),78 'values': variation.values.copy(),79 }80 @slash.parametrize('param', ['some_value'])81 def test_something(param, some_fixture):82 pass83 with slash.Session() as s:84 @s.fixture_store.add_fixture85 @slash.fixture86 @slash.parametrize('value', [value1])87 def some_fixture(value):88 return value289 s.fixture_store.resolve()90 with s.get_started_context():91 slash.runner.run_tests(make_runnable_tests(test_something))92 assert s.results.is_success(allow_skips=False)93 [info] = [result.data['variation_info'] for result in s.results.iter_test_results()]94 assert info['id']['param'] == 095 assert info['values']['param'] == 'some_value'96 assert info['id']['some_fixture.value'] == 097 assert 'some_fixture' not in info['values']98 assert info['values']['some_fixture.value'] == value199def _freeze(dictionary):100 return frozenset(dictionary.items())101def test_variation_tuples(results):102 [res] = results.test_parametrization_tuple103 values = res.data['captured_values']104 assert values['x'] == 1105 assert values['y'] == 2106def test_nested_fixture_ids(results):107 ids = {res.data['captured_values']['outer_fixture.outer_param'] for res in results.test_nested_fixture}108 assert ids == {666}109 for res in results.test_nested_fixture:110 assert 'outer_fixture' not in res.data['captured_values']111def test_fixture_and_toggle(results):112 assert len(results.test_fixture_and_toggle) == 2113@pytest.fixture114def results():115 tests = []116 def include(f):117 tests.append(f)118 return f119 @include120 def test_no_params():121 pass122 @include123 def test_single_param_fixture(fixture):124 _capture_arguments()125 @include126 def test_nested_fixture(outer_fixture):127 _capture_arguments()128 @include129 @slash.parametrize(('x', 'y'), [(1, 2)])130 def test_parametrization_tuple(x, y):131 _capture_arguments()132 @include133 @slash.parameters.toggle('toggle')134 def test_fixture_and_toggle(fixture, toggle):135 _capture_arguments()136 with slash.Session() as s:137 @s.fixture_store.add_fixture138 @slash.fixture139 def fixture():140 return _object1141 @s.fixture_store.add_fixture142 @slash.fixture143 @slash.parametrize('x', [1, 2, 3])144 def inner_fixture(x):145 return 'inner{}'.format(x)146 @s.fixture_store.add_fixture147 @slash.fixture148 @slash.parametrize('outer_param', [666])149 def outer_fixture(inner_fixture, outer_param):150 return 'outer_{}'.format(inner_fixture)151 s.fixture_store.resolve()152 with s.get_started_context():153 slash.runner.run_tests(make_runnable_tests(tests))154 assert s.results.is_success(allow_skips=False)155 returned = collections.defaultdict(list)156 for res in s.results.iter_test_results():157 returned[res.test_metadata.function_name].append(res)158 return Munch(returned)159# helpers ################################################################################160_object1 = object()161def _capture_arguments():162 values = copy.copy(slash.context.result.test_metadata.variation.values)...
test_test.py
Source:test_test.py
...21 events.append("test_1")22 def test_2(self):23 events.append("test_2")24 with slash.Session():25 tests = make_runnable_tests(Test)26 for test in tests:27 self.assertIsInstance(test, Test)28 self.assertEqual(len(tests), 2)29 tests.sort(key=lambda test: test._test_method_name) # pylint: disable=protected-access30 for test in tests:31 test.run()32 self.assertEqual(events, ["before", "test_1", "after", "before", "test_2", "after"])33 def test_before_failures(self):34 "Check that exceptions during before() prevent after() from happening"35 events = []36 class Test(slash.Test):37 def before(self):38 raise CustomException()39 def test(self):40 events.append("test")41 def after(self):42 events.append("after")43 with slash.Session():44 [test] = make_runnable_tests(Test) # pylint: disable=unbalanced-tuple-unpacking45 with self.assertRaises(CustomException):46 test.run()47 self.assertEqual(events, [])48 def test_after_failures(self):49 class Test(slash.Test):50 def test(self):51 assert False, "msg1"52 def after(self):53 assert False, "msg2"54 session = run_tests_in_session(Test)55 self.assertFalse(session.results.is_success())56 [result] = session.results.iter_test_results()57 self.assertEqual(len(result.get_failures()), 2)58 def test_after_gets_called(self):59 "If before() is successful, after() always gets called"60 events = []61 class Test(slash.Test):62 def before(self):63 events.append("before")64 def test_1(self):65 events.append("test")66 raise CustomException(1)67 def after(self):68 events.append("after")69 with slash.Session():70 [test] = make_runnable_tests(Test) # pylint: disable=unbalanced-tuple-unpacking71 with self.assertRaises(CustomException):72 test.run()73 self.assertEqual(events, ["before", "test", "after"])74class AbstractTestTest(TestCase):75 def test_abstract_tests(self):76 @slash.abstract_test_class77 class Abstract(slash.Test):78 def test1(self):79 pass80 def test2(self):81 pass82 def test3(self):83 pass84 with slash.Session():85 self.assertEqual(list(make_runnable_tests(Abstract)), [])86 class Derived(Abstract):87 pass88 with slash.Session():89 self.assertEqual(len(list(make_runnable_tests(Derived))), 3)90class TestParametersTest(TestCase):91 def test_parameters(self):92 variations = []93 a_values = [1, 2]94 b_values = [3, 4]95 c_values = [5, 6]96 d_values = [7, 8]97 class Parameterized(slash.Test):98 @slash.parameters.iterate(a=a_values)99 def before(self, a): # pylint: disable=arguments-differ100 variations.append([a])101 @slash.parameters.iterate(b=b_values, c=c_values)102 def test(self, b, c):103 variations[-1].extend([b, c])104 @slash.parameters.iterate(d=d_values)105 def after(self, d): # pylint: disable=arguments-differ106 variations[-1].append(d)107 with slash.Session():108 for test in make_runnable_tests(Parameterized):109 test.run()110 self.assertEqual(111 set(tuple(x) for x in variations),112 set(itertools.product(113 a_values,114 b_values,115 c_values,116 d_values...
__init__.py
Source:__init__.py
...72 with ExitStack() as stack:73 if session is None:74 session = slash.Session()75 stack.enter_context(session)76 test_class_path_or_iterator = make_runnable_tests(test_class_path_or_iterator)77 with session.get_started_context():78 slash.runner.run_tests(test_class_path_or_iterator)79 for result in session.results.iter_test_results():80 for err in itertools.chain(result.get_errors(), result.get_failures(), result.get_skips()):81 _logger.debug("Unsuccessful result: {0}", err)82 return session83run_tests_in_session.__test__ = False84def run_tests_assert_success(test_class_path_or_iterator, session=None):85 session = run_tests_in_session(test_class_path_or_iterator, session=session)86 assert session.results.is_success(), "Run did not succeed"87 return session88run_tests_assert_success.__test__ = False89def make_runnable_tests(thing):90 return slash.loader.Loader().get_runnables(thing)91def resolve_and_run(thing):92 slash.context.session.fixture_store.resolve()93 with slash.context.session.get_started_context():94 tests = make_runnable_tests(thing)95 slash.runner.run_tests(tests)96 return list(slash.context.session.results.iter_test_results())97def without_pyc(filename):98 if filename.endswith('.pyc'):99 return filename[:-1]100 return filename101def raises_maybe(exc, cond):102 @contextmanager103 def noop():104 yield105 if cond:106 return pytest.raises(exc)107 return noop()108_noop = lambda f: f...
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!!