Best Python code snippet using pytest-bdd_python
test_outline.py
Source:test_outline.py
...45 "outline.feature",46 "Outlined given, when, thens",47 example_converters=dict(start=int, eat=float, left=str)48 )49 def test_outline(request):50 assert get_parametrize_markers_args(request.node) == (51 ["start", "eat", "left"],52 [53 [12, 5.0, "7"],54 [5, 4.0, "1"],55 ],56 )57 """58 )59 )60 result = testdir.runpytest()61 result.assert_outcomes(passed=2)62def test_wrongly_outlined(testdir):63 """Test parametrized scenario when the test function lacks parameters."""64 testdir.makefile(65 ".feature",66 outline=textwrap.dedent(67 """\68 Feature: Outline69 Scenario Outline: Outlined with wrong examples70 Given there are <start> cucumbers71 When I eat <eat> cucumbers72 Then I should have <left> cucumbers73 Examples:74 | start | eat | left | unknown_param |75 | 12 | 5 | 7 | value |76 """77 ),78 )79 testdir.makeconftest(textwrap.dedent(STEPS))80 testdir.makepyfile(81 textwrap.dedent(82 """\83 from pytest_bdd import scenario84 @scenario("outline.feature", "Outlined with wrong examples")85 def test_outline(request):86 pass87 """88 )89 )90 result = testdir.runpytest()91 assert_outcomes(result, errors=1)92 result.stdout.fnmatch_lines(93 '*ScenarioExamplesNotValidError: Scenario "Outlined with wrong examples"*has not valid examples*',94 )95 result.stdout.fnmatch_lines("*should match set of example values [[]'eat', 'left', 'start', 'unknown_param'[]].*")96def test_wrong_vertical_examples_scenario(testdir):97 """Test parametrized scenario vertical example table has wrong format."""98 testdir.makefile(99 ".feature",100 outline=textwrap.dedent(101 """\102 Feature: Outline103 Scenario Outline: Outlined with wrong vertical example table104 Given there are <start> cucumbers105 When I eat <eat> cucumbers106 Then I should have <left> cucumbers107 Examples: Vertical108 | start | 12 | 2 |109 | start | 10 | 1 |110 | left | 7 | 1 |111 """112 ),113 )114 testdir.makeconftest(textwrap.dedent(STEPS))115 testdir.makepyfile(116 textwrap.dedent(117 """\118 from pytest_bdd import scenario119 @scenario("outline.feature", "Outlined with wrong vertical example table")120 def test_outline(request):121 pass122 """123 )124 )125 result = testdir.runpytest()126 assert_outcomes(result, errors=1)127 result.stdout.fnmatch_lines(128 "*Scenario has not valid examples. Example rows should contain unique parameters. "129 '"start" appeared more than once.*'130 )131def test_wrong_vertical_examples_feature(testdir):132 """Test parametrized feature vertical example table has wrong format."""133 testdir.makefile(134 ".feature",135 outline=textwrap.dedent(136 """\137 Feature: Outlines138 Examples: Vertical139 | start | 12 | 2 |140 | start | 10 | 1 |141 | left | 7 | 1 |142 Scenario Outline: Outlined with wrong vertical example table143 Given there are <start> cucumbers144 When I eat <eat> cucumbers145 Then I should have <left> cucumbers146 """147 ),148 )149 testdir.makeconftest(textwrap.dedent(STEPS))150 testdir.makepyfile(151 textwrap.dedent(152 """\153 from pytest_bdd import scenario154 @scenario("outline.feature", "Outlined with wrong vertical example table")155 def test_outline(request):156 pass157 """158 )159 )160 result = testdir.runpytest()161 assert_outcomes(result, errors=1)162 result.stdout.fnmatch_lines(163 "*Feature has not valid examples. Example rows should contain unique parameters. "164 '"start" appeared more than once.*'165 )166def test_outlined_with_other_fixtures(testdir):167 """Test outlined scenario also using other parametrized fixture."""168 testdir.makefile(169 ".feature",170 outline=textwrap.dedent(171 """\172 Feature: Outline173 Scenario Outline: Outlined given, when, thens174 Given there are <start> cucumbers175 When I eat <eat> cucumbers176 Then I should have <left> cucumbers177 Examples:178 | start | eat | left |179 | 12 | 5 | 7 |180 | 5 | 4 | 1 |181 """182 ),183 )184 testdir.makeconftest(textwrap.dedent(STEPS))185 testdir.makepyfile(186 textwrap.dedent(187 """\188 import pytest189 from pytest_bdd.utils import get_parametrize_markers_args190 from pytest_bdd import scenario191 @pytest.fixture(params=[1, 2, 3])192 def other_fixture(request):193 return request.param194 @scenario(195 "outline.feature",196 "Outlined given, when, thens",197 example_converters=dict(start=int, eat=float, left=str)198 )199 def test_outline(other_fixture):200 pass201 """202 )203 )204 result = testdir.runpytest()205 result.assert_outcomes(passed=6)206def test_vertical_example(testdir):207 """Test outlined scenario with vertical examples table."""208 testdir.makefile(209 ".feature",210 outline=textwrap.dedent(211 """\212 Feature: Outline213 Scenario Outline: Outlined with vertical example table214 Given there are <start> cucumbers215 When I eat <eat> cucumbers216 Then I should have <left> cucumbers217 Examples: Vertical218 | start | 12 | 2 |219 | eat | 5 | 1 |220 | left | 7 | 1 |221 """222 ),223 )224 testdir.makeconftest(textwrap.dedent(STEPS))225 testdir.makepyfile(226 textwrap.dedent(227 """\228 from pytest_bdd.utils import get_parametrize_markers_args229 from pytest_bdd import scenario230 @scenario(231 "outline.feature",232 "Outlined with vertical example table",233 example_converters=dict(start=int, eat=float, left=str)234 )235 def test_outline(request):236 assert get_parametrize_markers_args(request.node) == (237 ["start", "eat", "left"],238 [239 [12, 5.0, "7"],240 [2, 1.0, "1"],241 ],242 )243 """244 )245 )246 result = testdir.runpytest()247 result.assert_outcomes(passed=2)248def test_outlined_feature(testdir):249 testdir.makefile(250 ".feature",251 outline=textwrap.dedent(252 """\253 Feature: Outline254 Examples:255 | start | eat | left |256 | 12 | 5 | 7 |257 | 5 | 4 | 1 |258 Scenario Outline: Outlined given, when, thens259 Given there are <start> <fruits>260 When I eat <eat> <fruits>261 Then I should have <left> <fruits>262 Examples:263 | fruits |264 | oranges |265 | apples |266 """267 ),268 )269 testdir.makepyfile(270 textwrap.dedent(271 """\272 from pytest_bdd.utils import get_parametrize_markers_args273 from pytest_bdd import given, when, then, scenario274 @scenario(275 "outline.feature",276 "Outlined given, when, thens",277 example_converters=dict(start=int, eat=float, left=str)278 )279 def test_outline(request):280 assert get_parametrize_markers_args(request.node) == (281 ["start", "eat", "left"],282 [[12, 5.0, "7"], [5, 4.0, "1"]],283 ["fruits"],284 [["oranges"], ["apples"]],285 )286 @given("there are <start> <fruits>", target_fixture="start_fruits")287 def start_fruits(start, fruits):288 assert isinstance(start, int)289 return {fruits: dict(start=start)}290 @when("I eat <eat> <fruits>")291 def eat_fruits(start_fruits, eat, fruits):292 assert isinstance(eat, float)293 start_fruits[fruits]["eat"] = eat...
outlines.py
Source:outlines.py
...5])6def module_outline(self, name):7 note(name)8@TestOutline9def test_outline(self):10 note("outline")11@TestScenario12def scenario_using_outline(self):13 Example(run=test_outline)14@TestOutline(Scenario)15def scenario_outline(self):16 note("scenario outline")17@TestScenario18def scenario_using_scenario_outline(self):19 scenario_outline()20@TestScenario21def scenario_using_scenario_outline_for_example(self):22 Example(run=scenario_outline)23@TestScenario24@Examples("name value", [25 ("hi", "now"),26 ("bye", "then")27])28def scenario_with_examples(self):29 for example in self.examples:30 note(example)31@TestOutline32def outline_with_args(self, name, value=None):33 note(f"{name} {value}")34@TestOutline(Scenario)35def scenario_outline_with_args(self, name, value=None):36 note(f"{name} {value}")37@TestOutline(Suite)38def suite_outline_with_args(self, name, value=None):39 note(f"{name} {value}")40@TestScenario41@Examples("name value", [42 ("hi", "now"),43 ("bye", "then")44])45def scenario_with_examples_using_outline_with_args(self):46 note(f"scenario with examples using outline with args: {self.examples}")47 for example in self.examples:48 Example(name=example, test=scenario_outline_with_args, type=self.type)(**vars(example))49 break50@TestScenario51def scenario_with_inner_examples(self):52 if not self.examples:53 self.examples = Examples("name value", [54 ("hi","there"),55 ("bye", "here")56 ])57 for example in self.examples:58 Example(name=example, test=scenario_outline_with_args, type=self.type)(**vars(example))59@TestScenario60def scenario(self):61 note(f"{self} scenario")62@TestOutline(Scenario)63@Examples("name value", [64 ("hi","there"),65 ("bye", "here")66])67def scenario_outline_with_examples_and_args(self, name, value=None): 68 note(f"{self} {name} {value}")69 Scenario("my inner scenario", run=scenario_with_examples_using_outline_with_args)70with Module("regression"):71 with Outline("my outline"):72 note("hello")73 module_outline(name="hello")74 test_outline()75 scenario_using_outline()76 scenario_outline()77 scenario_using_scenario_outline()78 scenario_using_scenario_outline_for_example()79 scenario_with_examples()80 outline_with_args(name="hello")81 scenario_outline_with_args(name="hello")82 scenario_with_examples_using_outline_with_args()83 # override built-in examples of the outline84 Scenario(run=scenario_with_examples_using_outline_with_args, 85 examples=Examples("name value", [("hi","there")]))86 scenario_with_inner_examples()87 scenario_outline_with_examples_and_args()88 scenario_outline_with_examples_and_args(name="hello", value="there")...
test_01_outline_node.py
Source:test_01_outline_node.py
1import os2from unittest import TestCase3from outline.outline import Outline4from outline.outline_node import OutlineNode5import tests.test_utilities.test_config as tcfg6class TestOutlineNode(TestCase):7 local_path = os.path.join('outline', 'outline_node')8 test_outline = os.path.join(tcfg.input_files_root, local_path, 'outline-test-valid-01.opml')9 def test_child_access(self):10 outline = Outline.from_opml(self.test_outline)11 top_level_node = outline.top_outline_node12 num_child_nodes = len(top_level_node)13 self.assertEqual(3, num_child_nodes)14 child_outline_nodes = list(top_level_node)15 self.assertEqual(3, len(child_outline_nodes))16 for index, child in enumerate(top_level_node):17 self.assertIsInstance(child, OutlineNode)18 self.assertIsInstance(child_outline_nodes[index], OutlineNode)19 self.assertEqual(child, child_outline_nodes[index])20 def test_field_access(self):21 outline = Outline.from_opml(self.test_outline)22 top_level_node = outline.top_outline_node # Access the top level OutlineNode object23 # Check that accessing child node gets the right one24 node_01 = top_level_node[0]25 self.assertEqual('H1:Heading A', node_01.text)26 self.assertEqual('Notes for Heading A', node_01.note)27 # Check that accessing sub-nodes from top level works ok.28 # Note that (unleashed) tags are in the text but (correctly) not recognised by outline node.29 # Also note that white space is NOT ignored in the tag text as it isn't recognised as a tag.30 node_01_01 = top_level_node[0][0]31 self.assertEqual(' (-TAG-TEXT-H2B-)H2: Heading B', node_01_01.text)32 self.assertEqual('Notes for Heading C', node_01_01.note)33 # Check that two ways of getting to the same node reveal the same one.34 node_01_01_01a = node_01_01[0]35 node_01_01_01b = top_level_node[0][0][0]...
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!!