Best Python code snippet using pytest-bdd_python
the_fixture.py
Source:the_fixture.py
1"""Load and manipulate fixtures."""2import json3import os.path4import importlib_resources5from behave import given, step, then # pylint:disable=no-name-in-module6from tests.bdd.step_context import StepContext7class TheFixture(StepContext):8 MISSING = "*MISSING*"9 NONE = "*NONE*"10 context_key = "the_fixture"11 def __init__(self, **kwargs):12 super().__init__(**kwargs)13 self.base_dir = None14 self.fixtures = {}15 def set_base_dir(self, base_dir):16 base_dir = base_dir.lstrip("/")17 self.base_dir = str(18 importlib_resources.files("tests") / "bdd/fixtures" / base_dir19 )20 if not os.path.isdir(self.base_dir):21 raise EnvironmentError(f"Cannot find fixture dir: {self.base_dir}")22 def set_fixture(self, name, value):23 self.fixtures[name] = value24 return value25 def set_fixture_value(self, name, key, value):26 fixture = self.get_fixture(name)27 if value == self.MISSING:28 fixture.pop(key, None)29 return30 fixture[key] = None if value == self.NONE else value31 def get_fixture(self, name):32 return self.fixtures[name]33 def load_ini(self, filename, fixture_name):34 values = {}35 with open(self.get_path(filename), encoding="utf8") as handle:36 for line in handle:37 line = line.strip()38 if not line or line.startswith("#"):39 continue40 key, value = line.split("=", 1)41 values[key] = value42 return self.set_fixture(fixture_name, values)43 def get_path(self, filename):44 return os.path.join(self.base_dir, filename)45 def do_teardown(self):46 self.fixtures = {}47@given("fixtures are located in '{location}'")48def fixture_location(context, location):49 context.the_fixture.set_base_dir(location)50@given("I load the fixture '{fixture_file}.ini' as '{fixture_name}'")51def load_ini_fixture(context, fixture_file, fixture_name):52 context.the_fixture.load_ini(fixture_file + ".ini", fixture_name)53@given("I set the fixture '{fixture_name}' key '{key}' to '{value}'")54def set_fixture_value(context, fixture_name, key, value):55 context.the_fixture.set_fixture_value(fixture_name, key, value)56@given("I update the fixture '{fixture_name}' with")57def update_fixture_from_table(context, fixture_name):58 for row in context.table:59 set_fixture_value(context, fixture_name, row[0].strip(), row[1].strip())60@given("I update the fixture '{fixture_name}' from fixture '{other_fixture}'")61def update_fixture_from_fixture(context, fixture_name, other_fixture):62 the_fixture = context.the_fixture63 the_fixture.get_fixture(fixture_name).update(the_fixture.get_fixture(other_fixture))64@then("the fixture '{fixture_name}' key '{key}' is the value")65def set_fixture_key_to_the_value(context, fixture_name, key):66 context.the_value = context.the_fixture.get_fixture(fixture_name)[key]67def diff_dicts(a, b, missing=KeyError):68 return {69 key: (a.get(key, missing), b.get(key, missing))70 for key in dict(set(a.items()) ^ set(b.items())).keys()71 }72@then("the fixture '{fixture_name}' matches the fixture '{other_fixture}'")73def compare_fixtures(context, fixture_name, other_fixture):74 diff = diff_dicts(75 context.the_fixture.get_fixture(fixture_name),76 context.the_fixture.get_fixture(other_fixture),77 missing=TheFixture.MISSING,78 )79 if not diff:80 return81 for key, (value_found, value_expected) in diff.items():82 print(f"Key {key} is different. Found {value_found} expected {value_expected}")83 assert diff == {}, "The fixtures differ"84@step("I dump the fixture '{fixture_name}'")85def dump_fixture(context, fixture_name):86 fixture = context.the_fixture.get_fixture(fixture_name)87 print(f"Fixture '{fixture_name}'")...
test_yield_fixture.py
Source:test_yield_fixture.py
...12 inner_fixture_value = uuid4()13 with slash.Session() as s:14 @s.fixture_store.add_fixture15 @slash.fixture16 def other_fixture():17 return inner_fixture_value18 @s.fixture_store.add_fixture19 @slash.parametrize('iteration', list(iterations))20 @yield_fixture_decorator21 def fixture(iteration, other_fixture):22 assert other_fixture == inner_fixture_value23 start_checkpoints[iteration]()24 yield value25 end_checkpoints[iteration]()26 def test_something(fixture):27 assert fixture == value28 s.fixture_store.resolve()29 with s.get_started_context():30 slash.runner.run_tests(make_runnable_tests(test_something))...
test_wombats.py
Source:test_wombats.py
1def test_wombats(common_fixture):2 assert common_fixture == 'DATA'3def test_bread(other_fixture):...
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!!