@@ -561,15 +561,15 @@ Example:
561
561
assert cucumbers[" start" ] - cucumbers[" eat" ] == left
562
562
563
563
564
- Using the datatable Fixture
565
- ----------------------------
564
+ Step Definitions and Accessing the Datatable
565
+ --------------------------------------------
566
566
567
- The ``datatable `` fixture allows you to utilise data tables defined in your Gherkin scenarios
567
+ The ``datatable `` argument allows you to utilise data tables defined in your Gherkin scenarios
568
568
directly within your test functions. This is particularly useful for scenarios that require tabular data as input,
569
569
enabling you to manage and manipulate this data conveniently.
570
570
571
- The ``datatable `` fixture in pytest-bdd allows you to access the data tables defined in your Gherkin scenarios.
572
- When you use the ``datatable `` fixture in a step definition, it will return the table as a list of lists,
571
+ The ``datatable `` argument in pytest-bdd allows you to access the datatables defined in your Gherkin scenarios.
572
+ When you use the ``datatable `` argument in a step definition, it will return the table as a list of lists,
573
573
where each inner list represents a row from the table.
574
574
575
575
For example, the Gherkin table:
@@ -579,7 +579,7 @@ For example, the Gherkin table:
579
579
| name | email |
580
580
581
581
582
- Will be returned by the ``datatable `` fixture as:
582
+ Will be returned by the ``datatable `` argument as:
583
583
584
584
.. code-block :: python
585
585
@@ -588,54 +588,70 @@ Will be returned by the ``datatable`` fixture as:
588
588
589
589
]
590
590
591
- .. NOTE :: When using the datatable fixture , it is essential to ensure that the step to which it is applied
591
+ .. NOTE :: When using the datatable argument , it is essential to ensure that the step to which it is applied
592
592
actually has an associated data table. If the step does not have an associated data table,
593
- attempting to use the datatable fixture will raise an error.
593
+ attempting to use the datatable argument will raise an error.
594
594
Make sure that your Gherkin steps correctly reference the data table when defined.
595
595
596
596
Full example:
597
597
598
598
.. code-block :: gherkin
599
599
600
- Feature: User roles and permissions
600
+ Feature: Manage user accounts
601
601
602
- Scenario: Assigning roles to a user
602
+ Scenario: Creating a new user with roles and permissions
603
603
Given the following user details:
604
- | name | email |
605
-
604
+ | name | email | age |
605
+
606
+ | Alice | [email protected] | 25 |
607
+
608
+ When each user is assigned the following roles:
609
+ | Admin | Full access to the system |
610
+ | Contributor | Can add content |
606
611
607
- When the user is assigned the following roles:
608
- | role |
609
- | Admin |
610
- | Editor |
612
+ And the page is saved
611
613
612
614
Then the user should have the following permissions:
613
- | permission | allowed |
614
- | view content | true |
615
- | edit content | true |
616
- | delete content| false |
615
+ | permission | allowed |
616
+ | view dashboard | true |
617
+ | edit content | true |
618
+ | delete content | false |
617
619
618
620
.. code-block :: python
619
621
620
622
from pytest_bdd import given, when, then
621
623
622
- @given (" the following user details:" )
623
- def _ (datatable ):
624
- assert datatable == [[" name" , " email" ], [" John" , " [email protected] " ]]
625
624
626
- @when (" the user is assigned the following roles: " )
625
+ @given (" the following user details: " , target_fixture = " users " )
627
626
def _ (datatable ):
628
- assert datatable == [[" role" ], [" Admin" ], [" Editor" ]]
627
+ users = []
628
+ for row in datatable[1 :]:
629
+ users.append(row)
630
+
631
+ print (users)
632
+ return users
633
+
634
+
635
+ @when (" each user is assigned the following roles:" )
636
+ def _ (datatable , users ):
637
+ roles = datatable
638
+ for user in users:
639
+ for role_row in datatable:
640
+ assign_role(user, role_row)
641
+
642
+
643
+ @when (" the page is saved" )
644
+ def _ ():
645
+ save_page()
646
+
629
647
630
648
@then (" the user should have the following permissions:" )
631
- def _ (datatable ):
632
- expected_permissions = [
633
- [" permission" , " allowed" ],
634
- [" view content" , " true" ],
635
- [" edit content" , " true" ],
636
- [" delete content" , " false" ]
637
- ]
638
- assert datatable == expected_permissions
649
+ def _ (datatable , users ):
650
+ expected_permissions = []
651
+ for row in datatable[1 :]:
652
+ expected_permissions.append(row)
653
+
654
+ assert users_have_correct_permissions(users, expected_permissions)
639
655
640
656
641
657
Organizing your scenarios
0 commit comments