Best Python code snippet using slash
test_fixture_mechanism.py
Source:test_fixture_mechanism.py
...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 pass...
test_fixture_string_representation.py
Source:test_fixture_string_representation.py
...8from slash.utils.iteration import iter_cartesian_dicts9from .utils.code_formatter import CodeFormatter10def test_safe_repr_parameters(fixture_store, parametrized_func, params, param_names):11 variations = list(12 fixture_store.iter_parametrization_variations(funcs=[parametrized_func]))13 cartesian_product = list(iter_cartesian_dicts(params))14 assert len(variations) == len(cartesian_product)15 variation_names = set(str(v.safe_repr) for v in variations)16 assert len(variation_names) == len(cartesian_product)17 assert variation_names == set(','.join('{}={}'.format(name, combination[name]) for name in sorted(param_names))18 for combination in cartesian_product)19def test_safe_repr_fixtures(fixture_store):20 first_param_values = [1, 2, 3]21 second_param_values = [4, 5, 6]22 @fixture_store.add_fixture23 @slash.fixture24 @slash.parametrize('value', first_param_values)25 def first_fixture(value): # pylint: disable=unused-variable26 return value27 @fixture_store.add_fixture28 @slash.fixture29 @slash.parametrize('value', second_param_values)30 def second_fixture(value): # pylint: disable=unused-variable31 return value32 def test_func(first_fixture, second_fixture): # pylint: disable=unused-argument33 pass34 fixture_store.resolve()35 variations = list(36 fixture_store.iter_parametrization_variations(funcs=[test_func]))37 assert len(variations) == len(first_param_values) * len(second_param_values)38 assert set(str(variation.safe_repr) for variation in variations) == {39 'first_fixture.value={},second_fixture.value={}'.format(i, j)40 for i, j in itertools.product(first_param_values, second_param_values)41 }42@pytest.fixture43def parametrized_func(params, param_names):44 buff = StringIO()45 formatter = CodeFormatter(buff)46 formatter.writeln('def f({}):'.format(', '.join(param_names)))47 with formatter.indented():48 formatter.writeln('pass')49 globs = {}50 exec(buff.getvalue(), globs) # pylint: disable=exec-used...
function_test.py
Source:function_test.py
...39 super(FunctionTestFactory, self).__init__(func)40 self.func = func41 def _generate_tests(self, fixture_store):42 namespace = fixture_store.get_current_namespace()43 for variation in fixture_store.iter_parametrization_variations(funcs=[self.func]):...
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!!