Best Python code snippet using slash
test_fixtures.py
Source:test_fixtures.py
1import pytest2import operator3from functools import reduce4def test_fixtures(suite, suite_test, defined_fixture):5 suite_test.depend_on_fixture(defined_fixture)6 suite.run()7def test_fixture_cleanup_at_end_of_suite(suite):8 fixture = suite.slashconf.add_fixture()9 suite[-1].depend_on_fixture(fixture)10 cleanup = fixture.add_cleanup()11 summary = suite.run()12 assert cleanup in summary.events13def test_fixture_cleanup_failure_fails_test(suite, suite_test, defined_fixture):14 suite_test.depend_on_fixture(defined_fixture)15 defined_fixture.add_cleanup(extra_code=['raise Exception()'])16 suite_test.expect_error()17 suite.run()18def test_fixture_parameters(suite, suite_test, defined_fixture):19 defined_fixture.add_parameter()20 suite_test.depend_on_fixture(defined_fixture)21 summary = suite.run()22 all_results = summary.get_all_results_for_test(suite_test)23 num_combinations = reduce(operator.mul, (len(p.values) for p in defined_fixture.get_parameters()))24 assert len(all_results) == num_combinations25def test_fixture_dependency_chain(suite, suite_test):26 fixture1 = suite.slashconf.add_fixture()27 fixture1.add_parameter()28 fixture2 = suite.slashconf.add_fixture()29 fixture2.add_parameter()30 fixture2.depend_on_fixture(fixture1)31 suite_test.depend_on_fixture(fixture2)32 suite.run()33def test_fixture_dependency_both_directly_and_indirectly(suite, suite_test):34 fixture1 = suite.slashconf.add_fixture()35 num_values1 = 236 fixture1.add_parameter(num_values=num_values1)37 fixture2 = suite.slashconf.add_fixture()38 num_values2 = 339 fixture2.add_parameter(num_values=num_values2)40 fixture2.depend_on_fixture(fixture1)41 suite_test.depend_on_fixture(fixture1)42 suite_test.depend_on_fixture(fixture2)43 summary = suite.run()44 results = summary.get_all_results_for_test(suite_test)45 assert len(results) == num_values1 * num_values246def test_fixture_context(suite, suite_test):47 fixture1 = suite.slashconf.add_fixture()48 fixture1.append_line('assert this == slash.context.fixture')49 fixture2 = suite.slashconf.add_fixture()50 fixture2.append_line('assert this == slash.context.fixture')51 fixture2.depend_on_fixture(fixture1)52 suite_test.depend_on_fixture(fixture1)53 suite.run()54@pytest.mark.parametrize('where', [['before'], ['after'], ['before', 'after']])55def test_fixtures_in_before_after(suite, where):56 test_class = suite.files[-1].add_class()57 suite_test = test_class.add_method_test()58 fixture = suite.slashconf.add_fixture()59 fixture_event = fixture.add_event(name='fixture')60 assert len(suite_test.cls.tests) == 161 before = suite_test.cls.add_before_method()62 evt1 = before.add_event(name='before')63 after = suite_test.cls.add_after_method()64 evt2 = after.add_event(name='after')65 for func in before, after:66 if func.name in where:67 func.depend_on_fixture(fixture)68 func.append_line('assert {} == {}'.format(fixture.name, fixture.get_value_string()))69 summary = suite.run()70 assert evt1 in summary.events71 assert evt2 in summary.events72 assert fixture_event in summary.events73def test_fixture_and_parameter(suite, suite_test, get_fixture_location):74 fixture = get_fixture_location(suite_test).add_fixture()75 suite_test.add_parameter()76 suite_test.depend_on_fixture(fixture)77 suite.run()78def test_fixture_which_name_startswith_test(suite):79 fixture = suite.slashconf.add_fixture(name='test_my_fixture')80 suite[-1].depend_on_fixture(fixture)81 for test in suite:82 test.expect_deselect()83 summary = suite.run(expect_session_errors=True)84 assert not summary.ok()85 for output in [summary.get_console_output(),86 str(summary.session.results.global_result.get_errors()[0])]:87 assert 'Invalid fixture name' in output88def test_fixture_with_parameters(suite_builder):89 @suite_builder.first_file.add_code90 def __code__(): # pylint: disable=unused-variable91 import slash92 @slash.fixture93 @slash.parameters.iterate(my_param=['a',])94 def fixture_1(my_param): # pylint: disable=unused-variable95 yield my_param96 @slash.parameters.iterate(my_param=['b'])97 @slash.fixture98 def fixture_2(my_param): # pylint: disable=unused-variable99 yield my_param100 def test_a(fixture_1, fixture_2): # pylint: disable=unused-variable101 assert fixture_1 == 'a'102 assert fixture_2 == 'b'...
test_local_config.py
Source:test_local_config.py
1# pylint: disable=redefined-outer-name2from uuid import uuid13import slash4import pytest5from sentinels import NOTHING6from slash.core.local_config import LocalConfig7def test_local_conf(local_conf, expected_dict):8 conf = local_conf.get_dict()9 for key, value in expected_dict.items():10 assert [value] == conf[key]11def test_local_conf_loading_multiple_times(local_conf_dir, expected_dict):12 items = list(expected_dict.items())13 local_conf = LocalConfig()14 local_conf.push_path(str(local_conf_dir.join('..')))15 var_name, var_value = items[0]16 var = local_conf.get_dict()[var_name]17 assert var == [var_value]18 local_conf.push_path(str(local_conf_dir))19 assert var is local_conf.get_dict()[var_name]20def test_local_conf_nonexistent_dir():21 with pytest.raises(RuntimeError):22 LocalConfig().push_path('/nonexistent/dir')23def test_local_conf_with_both_slashconf_file_and_dir(tmpdir):24 with tmpdir.join('slashconf.py').open('w') as f:25 f.write("x = 1")26 conf_dir = tmpdir.join("slashconf")27 conf_dir.mkdir()28 with tmpdir.join('slashconf.py').open('w') as f:29 f.write("y = 2")30 local_config = LocalConfig()31 with pytest.raises(AssertionError) as caught:32 local_config.push_path(str(tmpdir))33 assert str(tmpdir) in str(caught.value)34def test_local_conf_with_slashconf_dir(tmpdir):35 conf_dir = tmpdir.join("slashconf")36 conf_dir.mkdir()37 with conf_dir.join('some_file.py').open('w') as f:38 f.write("x = 42")39 with conf_dir.join("other_file.py").open("w") as f:40 f.write("a = 1\n")41 f.write("b = 2")42 with conf_dir.join("__init__.py").open("w") as f:43 f.write("import slash\n")44 f.write("c = 1")45 local_config = LocalConfig()46 local_config.push_path(str(tmpdir))47 expected = {"a": 1, "b": 2, "c": 1, "x": 42, "slash": slash}48 config_dict = local_config.get_dict()49 got = {key: config_dict.get(key, NOTHING) for key in expected}50 assert expected == got51@pytest.fixture52def local_conf(local_conf_dir):53 returned = LocalConfig()54 returned.push_path(str(local_conf_dir))55 return returned56@pytest.fixture(params=[True, False])57def local_conf_dir(request, tmpdir, expected_dict):58 root_dir = returned = tmpdir59 is_conf_dir = request.param60 if is_conf_dir:61 returned = conf_dir = root_dir.join("slashconf")62 returned.mkdir()63 for i, (key, value) in enumerate(expected_dict.items()):64 returned = returned.join('subdir{}'.format(i))65 returned.mkdir()66 filename = f"{i}.py" if is_conf_dir else "slashconf.py"67 with returned.join(filename).open('w') as f:68 f.write('{} = [{!r}]'.format(key, value))69 if is_conf_dir:70 return conf_dir71 return returned72@pytest.fixture73def expected_dict():...
local_config.py
Source:local_config.py
1import os2import dessert3from emport import import_file4from ..utils.python import check_duplicate_functions5class LocalConfig(object):6 def __init__(self):7 super(LocalConfig, self).__init__()8 self._slashconf_vars_cache = {}9 self._configs = []10 self.duplicate_funcs = set()11 def push_path(self, path):12 self._configs.append(self._build_config(path))13 def pop_path(self):14 self._configs.pop(-1)15 def get_dict(self):16 return self._configs[-1]17 def _iter_slashconf_paths(self, dir_path):18 slashconf_path = os.path.join(dir_path, 'slashconf.py')19 slashconf_dir = os.path.join(dir_path, 'slashconf')20 if os.path.isfile(slashconf_path):21 assert not os.path.isdir(slashconf_dir), \22 f"Cannot use both file and directory for configuration in the same path ({dir_path!r})"23 yield slashconf_path24 elif os.path.isdir(slashconf_dir):25 for root, _, files in os.walk(slashconf_dir):26 for file_name in files:27 if file_name.endswith(".py"):28 yield os.path.join(root, file_name)29 def _build_config(self, path):30 confstack = []31 for dir_path in self._traverse_upwards(path):32 slashconf_vars = self._slashconf_vars_cache.get(dir_path)33 if slashconf_vars is None:34 slashconf_vars = {}35 for slashconf_path in self._iter_slashconf_paths(dir_path):36 self.duplicate_funcs |= check_duplicate_functions(slashconf_path)37 with dessert.rewrite_assertions_context():38 slashconf_vars.update(vars(import_file(slashconf_path)))39 if slashconf_vars:40 confstack.append(slashconf_vars)41 self._slashconf_vars_cache[dir_path] = slashconf_vars42 returned = {}43 # start loading from the parent so that vars are properly overriden44 for slashconf_vars in reversed(confstack):45 returned.update(slashconf_vars)46 return returned47 def _traverse_upwards(self, path):48 path = os.path.abspath(path)49 if not os.path.exists(path):50 raise RuntimeError("Path doesn't exist: {}".format(path))51 if os.path.isfile(path):52 path = os.path.dirname(path)53 upward_limit = os.path.splitdrive(os.path.normcase(os.path.abspath(os.path.sep)))[1]54 while True:55 yield path56 if os.path.splitdrive(os.path.normcase(path))[1] == upward_limit:57 break58 new_path = os.path.dirname(path)59 assert new_path != path...
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!!