How to use get_fixture_by_name method in Slash

Best Python code snippet using slash

fixture_store.py

Source:fixture_store.py Github

copy

Full Screen

...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:230 if isinstance(element, tuple):231 required_name, real_name = element232 else:233 required_name = real_name = element234 if required_name in skip_names:235 continue236 if element == 'this':237 continue238 fixture = namespace.get_fixture_by_name(real_name)239 fixtures_set.add((required_name, fixture))240 if hasattr(fixture.fixture_func, "__extrafixtures__"):241 self._get_fixtures_set(fixture.fixture_func.__extrafixtures__, skip_names=skip_names,242 namespace=namespace, fixtures_set=fixtures_set)243 return fixtures_set244 def get_fixture_value(self, fixture, name=None):245 if name is None:246 name = fixture.info.name247 value = self._compute_fixture_value(name, fixture)248 return value249 def get_value(self, variation, parameter_or_fixture):250 fixture_id = parameter_or_fixture.info.id251 fixtureobj = self.get_fixture_by_id(parameter_or_fixture.info.id)252 if isinstance(fixtureobj, Parametrization):...

Full Screen

Full Screen

test_fixture_mechanism.py

Source:test_fixture_mechanism.py Github

copy

Full Screen

...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_name287def test_fixture_store_add(fixture_func, fixture_func_name):288 # pylint: disable=no-member289 f = FixtureStore()290 assert f.add_fixture(fixture_func) is fixture_func291 assert f.get_fixture_by_name(292 fixture_func_name).fixture_func is fixture_func293def test_nofixtures_decorator(store):294 @slash.nofixtures295 def func1(a, b, c):296 pass297 [var] = store.iter_parametrization_variations(funcs=[func1])298 assert var == {}299def test_nofixtures_decorator_methods(store):300 class TestClass(slash.Test):301 @slash.nofixtures302 def before(self, a, b, c): # pylint: disable=arguments-differ303 pass304 [var] = store.iter_parametrization_variations(methods=[('before', TestClass.before)])305 assert var == {}306def test_parametrized_fixture(store):307 _fixture = lambda f: store.add_fixture(slash.fixture(f))308 @_fixture309 @slash.parametrize('a', [1, 2, 3])310 @slash.parametrize('b', [4, 5, 6])311 def fixture(a, b):312 pass313 store.resolve()314@pytest.mark.parametrize('use_dict', [True, False])315def test_fixture_name_override(store, use_dict):316 @slash.fixture(name='custom')317 def some_fixture():318 pass319 if use_dict:320 store.add_fixtures_from_dict({321 'some_fixture': some_fixture,322 'bla': None,323 'unused': 2,324 })325 else:326 store.add_fixture(some_fixture)327 store.resolve()328 assert store.get_fixture_by_name('custom').fixture_func is some_fixture329 with pytest.raises(UnknownFixtures):330 assert store.get_fixture_by_name('some_fixture')331@pytest.fixture(params=[True, False])332def fixture_func(request, fixture_func_name):333 def func(a, b, c, d):334 pass335 use_function_name = request.param336 if use_function_name:337 func.__name__ = fixture_func_name338 return slash.fixture(func)339 return slash.fixture(name=fixture_func_name)(func)340@pytest.fixture341def fixture_func_name():342 return str(uuid1()).replace('-', '_')343@pytest.fixture344def store():...

Full Screen

Full Screen

fixture.py

Source:fixture.py Github

copy

Full Screen

...34 return '<Function Fixture around {}>'.format(self.fixture_func)35 def is_override(self):36 parent = self.namespace.get_parent()37 while parent is not None:38 f = parent.get_fixture_by_name(self.info.name, default=None)39 if f is None:40 return False41 if f is not self:42 return True43 parent = parent.get_parent()44 return False45 def get_value(self, kwargs, active_fixture):46 if self.info.needs_this:47 assert 'this' not in kwargs48 kwargs['this'] = active_fixture49 with ExitStack() as stack:50 if context.session is not None:51 stack.enter_context(context.session.cleanups.default_scope_override(self.info.scope_name))52 return self.fixture_func(**kwargs)53 def get_requirements(self, store):54 fixture_requirements = get_requirements(self.fixture_func)55 required_fixtures = store.get_required_fixture_objects(self.fixture_func, self.namespace)56 while required_fixtures:57 fixture_requirements.extend(required_fixtures.pop().get_requirements(store))58 return fixture_requirements59 def _resolve(self, store):60 assert self.keyword_arguments is None61 assert self.parametrization_ids is None62 self.parametrization_ids = OrderedSet()63 keyword_arguments = OrderedDict()64 parametrized = set()65 for name, param in iter_parametrization_fixtures(self.fixture_func):66 store.register_fixture_id(param)67 parametrized.add(name)68 self.parametrization_ids.add(param.info.id)69 keyword_arguments[name] = param70 for param_name, arg in self.info.required_args.items():71 if param_name in parametrized:72 continue73 try:74 needed_fixture = self.namespace.get_fixture_by_name(get_real_fixture_name_from_argument(arg))75 if needed_fixture.scope < self.scope: # pylint: disable=no-member76 raise InvalidFixtureScope('Fixture {} is dependent on {}, which has a smaller scope ({} > {})'.format(77 self.info.name, param_name, self.scope, needed_fixture.scope)) # pylint: disable=no-member78 if needed_fixture is self:79 raise CyclicFixtureDependency('Cyclic fixture dependency detected in {}: {} depends on itself'.format(80 self.info.func.__code__.co_filename,81 self.info.name))82 keyword_arguments[param_name] = needed_fixture83 except LookupError:84 raise UnknownFixtures(param_name)...

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