Best Python code snippet using slash
fixture_store.py
Source:fixture_store.py
...38 for f in self.iter_all_needed_fixture_objects(p))39 known = self._known_fixture_ids[p.info.id]40 return known.setdefault(combination, len(known))41 def iter_all_needed_fixture_objects(self, fixtureobj):42 for fid in self.get_all_needed_fixture_ids(fixtureobj):43 yield self.get_fixture_by_id(fid)44 def iter_active_fixtures(self):45 for _, fixtures in self._active_fixtures_by_scope.items():46 for f in fixtures.values():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:...
test_fixture_mechanism.py
Source:test_fixture_mechanism.py
...161 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):...
variation_factory.py
Source:variation_factory.py
...76 def iter_variations(self):77 needed_ids = OrderedSet()78 self._needed_fixtures.sort(key=lambda x: x.info.scope, reverse=True)79 for fixture in self._needed_fixtures:80 needed_ids.update(self._store.get_all_needed_fixture_ids(fixture))81 parametrizations = [self._store.get_fixture_by_id(param_id) for param_id in needed_ids]82 if not needed_ids:83 yield Variation(self._store, {}, {})84 return85 for value_indices in itertools.product(*(range(len(p.values)) for p in parametrizations)):86 yield self._build_variation(parametrizations, value_indices)87 def _build_variation(self, parametrizations, value_indices):88 value_index_by_id = {}89 for param, param_index in zip(parametrizations, value_indices):90 value_index_by_id[param.info.id] = param_index...
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!!