How to use get_fixture_dict method in Slash

Best Python code snippet using slash

fixture_store.py

Source:fixture_store.py Github

copy

Full Screen

...47 yield f48 def call_with_fixtures(self, test_func, namespace, trigger_test_start=False, trigger_test_end=False):49 if not nofixtures.is_marked(test_func):50 fixture_names = self.get_required_fixture_names(test_func)51 kwargs = self.get_fixture_dict(fixture_names, namespace)52 used_fixtures_decorator_names = getattr(test_func, '__extrafixtures__', None)53 if used_fixtures_decorator_names is not None:54 used_fixture_names_only = set(used_fixtures_decorator_names) - set(fixture_names)55 for name, fixture in self._get_fixtures_set(used_fixture_names_only, namespace=namespace):56 self.get_fixture_value(fixture, name=name)57 else:58 kwargs = {}59 try:60 if trigger_test_start:61 for fixture in self.iter_active_fixtures():62 fixture.call_test_start()63 return test_func(**kwargs)64 finally:65 if trigger_test_end:66 for fixture in self.iter_active_fixtures():67 with handling_exceptions(swallow=True):68 fixture.call_test_end()69 def get_required_fixture_names(self, test_func):70 """Returns a list of fixture names needed by test_func.71 Each element returned is either a string or a tuple of (required_name, real_name)72 """73 skip_names = {name for name, _ in iter_parametrization_fixtures(test_func)}74 returned = []75 for argument in get_arguments(test_func):76 if argument.name in skip_names:77 continue78 real_name = get_real_fixture_name_from_argument(argument)79 if real_name == argument.name:80 returned.append(real_name)81 else:82 returned.append((argument.name, real_name))83 return returned84 def get_required_fixture_objects(self, test_func, namespace):85 names = self.get_required_fixture_names(test_func)86 assert isinstance(names, list)87 return set(self.get_fixture_dict(names, namespace=namespace, get_values=False).values())88 def resolve_name(self, parameter_name, start_point, namespace=None):89 if namespace is None:90 namespace = self.get_current_namespace()91 parts = parameter_name.split('.')[::-1]92 if not parts:93 raise UnknownFixtures(parameter_name)94 while parts:95 current_name = parts.pop()96 param_fixtures = dict(iter_parametrization_fixtures(start_point))97 if current_name in param_fixtures:98 if parts: # we cannot decend further than a parameter99 raise UnknownFixtures(parameter_name)100 start_point = param_fixtures[current_name]101 else:102 start_point = self.get_fixture_by_name(current_name, namespace=namespace)103 namespace = start_point.namespace104 return start_point105 def __iter__(self):106 return iter(self._fixtures_by_id.values())107 def push_namespace(self):108 self._namespaces.append(Namespace(self, parent=self._namespaces[-1]))109 def pop_namespace(self):110 return self._namespaces.pop(-1)111 @contextmanager112 def new_namespace_context(self):113 self.push_namespace()114 try:115 yield116 finally:117 self.pop_namespace()118 def get_current_namespace(self):119 return self._namespaces[-1]120 def get_all_needed_fixture_ids(self, fixtureobj):121 if self._unresolved_fixture_ids:122 raise UnresolvedFixtureStore()123 if isinstance(fixtureobj, Parametrization):124 return frozenset([fixtureobj.info.id])125 returned = self._all_needed_parametrization_ids_by_fixture_id.get(fixtureobj.info.id)126 if returned is None:127 returned = self._compute_all_needed_parametrization_ids(fixtureobj)128 self._all_needed_parametrization_ids_by_fixture_id[fixtureobj.info.id] = returned129 return returned130 def iter_autouse_fixtures_in_namespace(self, namespace=None):131 if namespace is None:132 namespace = self.get_current_namespace()133 for fixture in namespace.iter_fixtures():134 if fixture.info.autouse:135 yield fixture136 def activate_autouse_fixtures_in_namespace(self, namespace):137 for fixture in self.iter_autouse_fixtures_in_namespace(namespace):138 _ = self.get_fixture_value(fixture)139 def _compute_all_needed_parametrization_ids(self, fixtureobj):140 stack = [(fixtureobj.info.id, [fixtureobj.info.id], set([fixtureobj.info.id]))]141 returned = OrderedSet()142 while stack:143 fixture_id, path, visited = stack.pop()144 if fixture_id in self._all_needed_parametrization_ids_by_fixture_id:145 returned.update(self._all_needed_parametrization_ids_by_fixture_id[fixture_id])146 continue147 fixture = self._fixtures_by_id[fixture_id]148 if fixture.parametrization_ids:149 assert isinstance(fixture.parametrization_ids, OrderedSet)150 returned.update(fixture.parametrization_ids)151 if fixture.keyword_arguments:152 for needed in fixture.keyword_arguments.values():153 if needed.is_parameter():154 continue155 needed_id = needed.info.id156 if needed_id in visited:157 self._raise_cyclic_dependency_error(fixtureobj, path, needed_id)158 stack.append((needed_id, path + [needed_id], visited | set([needed_id])))159 return returned160 def _raise_cyclic_dependency_error(self, fixtureobj, path, new_id):161 raise CyclicFixtureDependency(162 'Cyclic fixture dependency detected in {}: {}'.format(163 fixtureobj.info.func.__code__.co_filename,164 ' -> '.join(self._fixtures_by_id[f_id].info.name165 for f_id in path + [new_id])))166 def push_scope(self, scope):167 scope = get_scope_by_name(scope)168 def pop_scope(self, scope): # pylint: disable=unused-argument169 if slash_context.result is not None and slash_context.result.is_interrupted():170 return171 scope = get_scope_by_name(scope)172 for s, active_fixtures in self._active_fixtures_by_scope.items():173 if s <= scope:174 for active_fixture in reversed(list(active_fixtures.values())):175 with handling_exceptions(swallow=True):176 self._deactivate_fixture(active_fixture.fixture)177 assert not active_fixtures178 def ensure_known_parametrization(self, parametrization):179 if parametrization.info.id not in self._fixtures_by_id:180 self._fixtures_by_id[parametrization.info.id] = parametrization181 def add_fixtures_from_dict(self, d):182 for thing in d.values():183 fixture_info = getattr(thing, '__slash_fixture__', None)184 if fixture_info is None:185 continue186 assert self.get_current_namespace() is self._namespaces[-1]187 fixture_info = self.add_fixture(thing).__slash_fixture__188 self.get_current_namespace().add_name(189 fixture_info.name, fixture_info.id)190 def add_fixture(self, fixture_func):191 fixture_info = fixture_func.__slash_fixture__192 existing_fixture = self._fixtures_by_id.get(fixture_info.id)193 if existing_fixture is not None:194 return existing_fixture.fixture_func195 if is_valid_test_name(fixture_info.name):196 raise InvalidFixtureName('Invalid fixture name: {.name}'.format(fixture_info))197 fixture_object = Fixture(self, fixture_func)198 current_namespace = self._namespaces[-1]199 current_namespace.add_name(fixture_info.name, fixture_info.id)200 self.register_fixture_id(fixture_object)201 return fixture_func202 def register_fixture_id(self, f):203 if f.info.id in self._fixtures_by_id:204 return205 self._fixtures_by_id[f.info.id] = f206 self._unresolved_fixture_ids.add(f.info.id)207 def get_fixture_by_name(self, name, namespace=None):208 if namespace is None:209 namespace = self._namespaces[-1]210 return namespace.get_fixture_by_name(name)211 def get_fixture_by_argument(self, arg):212 return self.get_fixture_by_name(get_real_fixture_name_from_argument(arg))213 def get_fixture_by_id(self, fixture_id):214 return self._fixtures_by_id[fixture_id]215 def get_fixture_dict(self, fixture_names, namespace=None, get_values=True, skip_names=frozenset()):216 returned = {}217 if namespace is None:218 namespace = self.get_current_namespace()219 fixtures_set = self._get_fixtures_set(fixture_names, skip_names=skip_names, namespace=namespace)220 for required_name, fixture in fixtures_set:221 if get_values:222 fixture = self.get_fixture_value(fixture, name=required_name)223 if required_name in fixture_names or [element for element in fixture_names if required_name in element]:224 returned[required_name] = fixture225 return returned226 def _get_fixtures_set(self, fixture_names, namespace=None, skip_names=frozenset(), fixtures_set=None):227 if fixtures_set is None:228 fixtures_set = OrderedSet()229 for element in fixture_names:...

Full Screen

Full Screen

test_fixture_mechanism.py

Source:test_fixture_mechanism.py Github

copy

Full Screen

...19 @_fixture20 def fixture3():21 return next(generation)22 store.resolve()23 d = store.get_fixture_dict(['fixture1', 'fixture2', 'fixture3'])24 for i in [1, 2, 3]:25 assert d['fixture{}'.format(i)] == i26def test_fixture_id_remains_even_when_context_popped(store):27 @slash.fixture28 def fixture0():29 pass30 store.push_namespace()31 store.add_fixture(fixture0)32 store.resolve()33 fixture_obj = store.get_fixture_by_name('fixture0')34 assert fixture_obj.fixture_func is fixture035 fixture_id = fixture_obj.info.id36 assert store.get_fixture_by_id(fixture_id) is fixture_obj37 store.pop_namespace()38 with pytest.raises(UnknownFixtures):39 store.get_fixture_by_name('fixture0')40 assert store.get_fixture_by_id(fixture_id) is fixture_obj41def test_namespace_get_fixture_by_name_default(store):42 obj = object()43 assert store.get_current_namespace().get_fixture_by_name('nonexisting', default=obj) is obj44def test_namespace_get_fixture_by_name_no_default(store):45 ns = store.get_current_namespace()46 with pytest.raises(UnknownFixtures):47 ns.get_fixture_by_name('nonexisting')48def test_variations_no_names(store):49 assert list(store.iter_parametrization_variations([])) == [{}]50def test_adding_fixture_twice_to_store(store):51 @slash.fixture52 def fixture0():53 pass54 store.add_fixture(fixture0)55 fixtureobj = store.get_fixture_by_name('fixture0')56 store.add_fixture(fixture0)57 assert store.get_fixture_by_name('fixture0') is fixtureobj58def test_fixture_store_namespace_repr(store):59 @store.add_fixture60 @slash.fixture61 def fixture0():62 pass63 ns = store.get_current_namespace()64 assert str(ns) == repr(ns)65 assert repr(ns) == 'Fixture NS#0: fixture0'66def test_fixture_parameters(store):67 @store.add_fixture68 @slash.fixture69 def value(x, a):70 assert a == 'a', 'Fixture got unexpectedly overriden by parameter'71 return x72 @store.add_fixture73 @slash.fixture74 def a():75 return 'a'76 @store.add_fixture77 @slash.fixture78 @slash.parametrize('a', [1, 2, 3])79 def x(a, b):80 return (a, b)81 @store.add_fixture82 @slash.parametrize('b', [4, 5, 6])83 @slash.fixture84 def b(b):85 return b86 store.resolve()87 with slash.Session():88 variations = list(_get_all_values(store, 'value'))89 assert set(variations) == set(itertools.product([1, 2, 3], [4, 5, 6]))90def test_fixture_tuple_parameters(store):91 @store.add_fixture92 @slash.fixture93 @slash.parametrize(('a', 'b'), [(1, 2), (3, 4)])94 def x(a, b):95 return a + b96 store.resolve()97 with slash.Session():98 variations = list(_get_all_values(store, 'x'))99 assert variations == [3, 7]100def test_variation_equality(store):101 @store.add_fixture102 @slash.fixture103 @slash.parametrize('a', [1, 2, 3])104 def fixture(a):105 pass106 store.resolve()107 prev_variation = None108 for variation in store.iter_parametrization_variations(fixture_ids=[store.get_fixture_by_name('fixture').info.id]):109 assert variation == variation110 assert not (variation != variation) # pylint: disable=superfluous-parens111 assert variation != prev_variation112 assert not (variation == prev_variation) # pylint: disable=superfluous-parens113 prev_variation = variation114def _get_all_values(store, fixture_name):115 returned = []116 for variation in store.iter_parametrization_variations(fixture_ids=[store.get_fixture_by_name(fixture_name).info.id]):117 store.push_scope('test')118 with bound_parametrizations_context(variation, store, store.get_current_namespace()):119 returned.append(120 store.get_fixture_dict([fixture_name])[fixture_name])121 store.pop_scope('test')122 return returned123@pytest.mark.parametrize('scopes', [('module', 'test'), ('session', 'module'), ('session', 'test')])124def test_wrong_scoping(store, scopes):125 @store.add_fixture126 @slash.fixture(scope=scopes[0])127 def fixture1(fixture2):128 pass129 @store.add_fixture130 @slash.fixture(scope=scopes[1])131 def fixture2():132 pass133 with pytest.raises(InvalidFixtureScope):134 store.resolve()135def test_this_argument(store):136 @store.add_fixture137 @slash.fixture138 def sample(this, other):139 assert this.name == 'sample'140 assert other == 'ok_other'141 return 'ok_sample'142 @store.add_fixture143 @slash.fixture144 def other(this):145 assert this.name == 'other'146 return 'ok_other'147 store.resolve()148 assert store.get_fixture_dict(['sample']) == {149 'sample': 'ok_sample',150 }151def test_fixture_store_unresolved(store):152 @store.add_fixture153 @slash.fixture154 def some_fixture(a, b, c):155 return a + b + c156 with pytest.raises(UnresolvedFixtureStore):157 store.get_fixture_dict(['some_fixture'])158def test_fixture_store_resolve_missing_fixtures(store):159 @store.add_fixture160 @slash.fixture161 def some_fixture(a, b, c):162 return a + b + c163 with pytest.raises(UnknownFixtures):164 store.resolve()165def test_get_all_needed_fixture_ids(store):166 @store.add_fixture167 @slash.fixture168 @slash.parametrize('param', [1, 2, 3])169 def fixture1(param): # pylint: disable=unused-argument170 pass171 @store.add_fixture172 @slash.fixture173 def fixture2(fixture1): # pylint: disable=unused-argument174 pass175 @store.add_fixture176 @slash.fixture177 @slash.parametrize('param', [4, 5, 6])178 def fixture3(fixture2, param): # pylint: disable=unused-argument179 pass180 fixtureobj = store.get_fixture_by_id(fixture3.__slash_fixture__.id)181 with pytest.raises(UnresolvedFixtureStore):182 store.get_all_needed_fixture_ids(fixtureobj)183 store.resolve()184 assert len(set(store.get_all_needed_fixture_ids(fixtureobj))) == 2185def test_get_all_needed_fixture_ids_of_parametrization(store):186 @store.add_fixture187 @slash.fixture188 @slash.parametrize('param', [1, 2, 3])189 def fixture1(param): # pylint: disable=unused-argument190 pass191 fixtureobj = store.get_fixture_by_id(fixture1.__slash_fixture__.id)192 [(_, param_fixtureobj)] = iter_parametrization_fixtures(fixture1)193 with pytest.raises(UnresolvedFixtureStore):194 store.get_all_needed_fixture_ids(fixtureobj)195 store.resolve()196 needed = set(store.get_all_needed_fixture_ids(param_fixtureobj))197 assert len(needed) == 1198 assert needed == set([param_fixtureobj.info.id])199def test_fixture_store_iter_parametrization_variations_missing_fixtures(store):200 def test_func(needed_fixture):201 pass202 with pytest.raises(UnknownFixtures):203 list(store.iter_parametrization_variations(funcs=[test_func]))204def test_fixture_store_iter_parametrization_variations_unresolved(store):205 @store.add_fixture206 @slash.fixture207 @slash.parametrize('x', [1, 2, 3])208 def needed_fixture(x):209 pass210 def test_func(needed_fixture):211 pass212 with pytest.raises(UnresolvedFixtureStore):213 list(store.iter_parametrization_variations(funcs=[test_func]))214def test_fixture_dependency(store):215 counter = itertools.count()216 @store.add_fixture217 @slash.fixture218 def fixture1(fixture2):219 assert fixture2 == 'fixture2_value_0'220 return 'fixture1_value_{}'.format(next(counter))221 @store.add_fixture222 @slash.fixture223 def fixture2():224 return 'fixture2_value_{}'.format(next(counter))225 store.resolve()226 assert store.get_fixture_dict(['fixture1', 'fixture2']) == {227 'fixture1': 'fixture1_value_1',228 'fixture2': 'fixture2_value_0',229 }230def test_nested_store_resolution_activation(store):231 store.push_namespace()232 @store.add_fixture233 @slash.fixture234 def fixture0():235 return '0'236 store.push_namespace()237 @store.add_fixture238 @slash.fixture239 def fixture1(fixture0):240 assert fixture0 == '0'241 return '1'242 store.push_namespace()243 @store.add_fixture244 @slash.fixture245 def fixture2(fixture1, fixture0):246 assert fixture0 == '0'247 assert fixture1 == '1'248 return '2'249 store.resolve()250 assert store.get_fixture_dict(['fixture2']) == {251 'fixture2': '2'252 }253 store.pop_namespace()254 with pytest.raises(UnknownFixtures):255 store.get_fixture_dict(['fixture2'])256def test_fixture_dependency_cycle():257 store = FixtureStore()258 @store.add_fixture259 @slash.fixture260 def fixture1(fixture2):261 return 1262 @store.add_fixture263 @slash.fixture264 def fixture2(fixture3):265 return 2266 @store.add_fixture267 @slash.fixture268 def fixture3(fixture1):269 return 3270 store.resolve()271 with pytest.raises(CyclicFixtureDependency):272 store.get_fixture_dict(['fixture1'])273def test_fixture_decorator():274 def func(a, b, c):275 pass276 assert not hasattr(func, '__slash_fixture__')277 assert slash.fixture(func) is func278 assert func.__slash_fixture__ is not None # pylint: disable=no-member279def test_fixture_decorator_multiple_calls(fixture_func):280 fixture_info = fixture_func.__slash_fixture__281 assert slash.fixture(slash.fixture(fixture_func)) is fixture_func282 assert fixture_func.__slash_fixture__ is fixture_info283def test_fixture_required_fixtures(fixture_func):284 assert set(fixture_func.__slash_fixture__.required_args) == set(arg.name for arg in get_arguments(fixture_func))285def test_fixture_name(fixture_func, fixture_func_name):286 assert fixture_func.__slash_fixture__.name == fixture_func_name...

Full Screen

Full Screen

test_fixture_scoping.py

Source:test_fixture_scoping.py Github

copy

Full Screen

...77 self._required_names = []78 self._populate_fixtures()79 self._values = {}80 def check_values(self):81 values = self._fixture_store.get_fixture_dict(self._required_names)82 for required_name in self._required_names:83 assert values[required_name] is not None84 expected_value = self._values[required_name]85 assert values[required_name] == expected_value86 assert not (set(self._fixtures) - set(self._required_names)87 ).intersection(self._values), 'Non-necessary fixtures unexpectedly initialized!'88 def check_value(self, name, value):89 assert self._values[name] == value90 def make_value(self, name):91 assert name not in self._values, 'Fixture generated more than once! (scope={})'.format(92 get_scope_name_by_scope(self._fixtures[name].__slash_fixture__.scope))93 value = str(uuid1())94 self._values[name] = value95 return value...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Slash automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful