@@ -561,88 +561,83 @@ Example:
561
561
assert cucumbers[" start" ] - cucumbers[" eat" ] == left
562
562
563
563
564
- Using the data_table Fixture
564
+ Using the datatable Fixture
565
565
----------------------------
566
566
567
- The ``data_table `` fixture allows you to utilise data tables defined in your Gherkin scenarios
567
+ The ``datatable `` fixture 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
- .. NOTE :: When using the data_table fixture, it is essential to ensure that the step to which it is applied
572
- actually has an associated data table. If the step does not have an associated data table,
573
- attempting to use the data_table fixture will raise an error.
574
- Make sure that your Gherkin steps correctly reference the data table when defined.
575
-
576
- Example:
577
-
578
- .. code-block :: python
579
-
580
- import pytest
581
- from pytest_bdd import scenario, given, when, then, data_table
582
-
583
- # Define a Gherkin scenario in a feature file.
584
- @scenario (' my_feature.feature' , ' Process a data table' )
585
- def test_process_data_table ():
586
- pass
587
-
588
- @given (' I have the following data' )
589
- def step_with_data (data_table ):
590
- # The data_table fixture provides access to the data in a tabular format.
591
- data_dict = data_table.to_dict()
592
- print (data_dict) # Prints the data in dictionary format.
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,
573
+ where each inner list represents a row from the table.
593
574
594
- @then (' I can transpose the data' )
595
- def step_transpose (data_table ):
596
- transposed_table = data_table.transpose()
597
- transposed_dict = transposed_table.to_dict()
598
- print (transposed_dict) # Prints the transposed data in dictionary format.
575
+ For example, the Gherkin table:
599
576
577
+ .. code-block :: gherkin
600
578
601
- The ``to_dict() `` method converts the data table into a dictionary format
602
- where the keys are the headers from the first row of the data table,
603
- and the values are lists of values from each subsequent row.
579
+ | name | email |
580
+
604
581
605
- Example :
582
+ Will be returned by the `` datatable `` fixture as :
606
583
607
584
.. code-block :: python
608
585
609
- # Assuming your data table looks like this:
610
- # | Name | Age |
611
- # | Alice | 30 |
612
- # | Bob | 25 |
586
+ [
587
+ [ " name " , " email " ],
588
+
589
+ ]
613
590
614
- data_dict = data_table.to_dict()
615
- # The output will be:
616
- # {
617
- # 'Name': ['Alice', 'Bob'],
618
- # 'Age': ['30', '25']
619
- # }
591
+ .. NOTE :: When using the datatable fixture, it is essential to ensure that the step to which it is applied
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.
594
+ Make sure that your Gherkin steps correctly reference the data table when defined.
620
595
596
+ Full example:
621
597
622
- The ``transpose() `` method converts rows of the data table into columns, effectively flipping the data structure.
623
- The first row will become the first column, the second row becomes the second column, and so on.
598
+ .. code-block :: gherkin
624
599
625
- Example:
600
+ Feature: User roles and permissions
626
601
627
- .. code-block :: python
602
+ Scenario: Assigning roles to a user
603
+ Given the following user details:
604
+ | name | email |
605
+
628
606
629
- # Assuming your data table looks like this :
630
- # | Name | Age |
631
- # | Alice | 30 |
632
- # | Bob | 25 |
607
+ When the user is assigned the following roles :
608
+ | role |
609
+ | Admin |
610
+ | Editor |
633
611
634
- # When you call the transpose method:
635
- transposed_table = data_table.transpose()
612
+ Then the user should have the following permissions:
613
+ | permission | allowed |
614
+ | view content | true |
615
+ | edit content | true |
616
+ | delete content| false |
636
617
637
- # The transposed data will look like this:
638
- # [
639
- # ["Name", "Alice", "Bob"],
640
- # ["Age", 30, 25]
641
- # ]
618
+ .. code-block :: python
642
619
643
- # Printing the transposed data:
644
- for row in transposed_table.rows:
645
- print ([cell.value for cell in row.cells])
620
+ from pytest_bdd import given, when, then
621
+
622
+ @given (" the following user details:" )
623
+ def given_user_details (datatable ):
624
+ assert datatable == [[" name" , " email" ], [" John" , " [email protected] " ]]
625
+
626
+ # When step to capture assigned roles
627
+ @when (" the user is assigned the following roles:" )
628
+ def when_user_roles (datatable ):
629
+ assert datatable == [[" role" ], [" Admin" ], [" Editor" ]]
630
+
631
+ # Then step to validate permissions
632
+ @then (" the user should have the following permissions:" )
633
+ def then_user_permissions (datatable ):
634
+ permissions = [
635
+ [" permission" , " allowed" ],
636
+ [" view content" , " true" ],
637
+ [" edit content" , " true" ],
638
+ [" delete content" , " false" ]
639
+ ]
640
+ assert datatable == expected_permissions
646
641
647
642
648
643
Organizing your scenarios
0 commit comments