@@ -384,20 +384,21 @@ Multiline steps
384
384
385
385
As Gherkin, pytest-bdd supports multiline steps
386
386
(a.k.a. `Doc Strings <https://cucumber.io/docs/gherkin/reference/#doc-strings >`_).
387
- But in much cleaner and powerful way:
388
387
389
388
.. code-block :: gherkin
390
389
391
390
Feature: Multiline steps
392
391
Scenario: Multiline step using sub indentation
393
392
Given I have a step with:
393
+ """
394
394
Some
395
395
Extra
396
396
Lines
397
+ """
397
398
Then the text should be parsed with correct indentation
398
399
399
- A step is considered as a multiline one, if the **next ** line(s) after it's first line is indented relatively
400
- to the first line . The step name is then simply extended by adding further lines with newlines.
400
+ A step is considered as a multiline one, if the **next ** line(s) after it's first line is encapsulated by
401
+ triple quotes . The step name is then simply extended by adding further lines with newlines.
401
402
In the example above, the Given step name will be:
402
403
403
404
.. code-block :: python
@@ -511,6 +512,90 @@ Example:
511
512
assert cucumbers[" start" ] - cucumbers[" eat" ] == left
512
513
513
514
515
+ Using the data_table Fixture
516
+ ----------------------------
517
+
518
+ The ``data_table `` fixture allows you to utilise data tables defined in your Gherkin scenarios
519
+ directly within your test functions. This is particularly useful for scenarios that require tabular data as input,
520
+ enabling you to manage and manipulate this data conveniently.
521
+
522
+ .. NOTE :: When using the data_table fixture, it is essential to ensure that the step to which it is applied
523
+ actually has an associated data table. If the step does not have an associated data table,
524
+ attempting to use the data_table fixture will raise an error.
525
+ Make sure that your Gherkin steps correctly reference the data table when defined.
526
+
527
+ Example:
528
+
529
+ .. code-block :: python
530
+
531
+ import pytest
532
+ from pytest_bdd import scenario, given, when, then, data_table
533
+
534
+ # Define a Gherkin scenario in a feature file.
535
+ @scenario (' my_feature.feature' , ' Process a data table' )
536
+ def test_process_data_table ():
537
+ pass
538
+
539
+ @given (' I have the following data' )
540
+ def step_with_data (data_table ):
541
+ # The data_table fixture provides access to the data in a tabular format.
542
+ data_dict = data_table.to_dict()
543
+ print (data_dict) # Prints the data in dictionary format.
544
+
545
+ @then (' I can transpose the data' )
546
+ def step_transpose (data_table ):
547
+ transposed_table = data_table.transpose()
548
+ transposed_dict = transposed_table.to_dict()
549
+ print (transposed_dict) # Prints the transposed data in dictionary format.
550
+
551
+
552
+ The ``to_dict() `` method converts the data table into a dictionary format
553
+ where the keys are the headers from the first row of the data table,
554
+ and the values are lists of values from each subsequent row.
555
+
556
+ Example:
557
+
558
+ .. code-block :: python
559
+
560
+ # Assuming your data table looks like this:
561
+ # | Name | Age |
562
+ # | Alice | 30 |
563
+ # | Bob | 25 |
564
+
565
+ data_dict = data_table.to_dict()
566
+ # The output will be:
567
+ # {
568
+ # 'Name': ['Alice', 'Bob'],
569
+ # 'Age': ['30', '25']
570
+ # }
571
+
572
+
573
+ The ``transpose() `` method converts rows of the data table into columns, effectively flipping the data structure.
574
+ The first row will become the first column, the second row becomes the second column, and so on.
575
+
576
+ Example:
577
+
578
+ .. code-block :: python
579
+
580
+ # Assuming your data table looks like this:
581
+ # | Name | Age |
582
+ # | Alice | 30 |
583
+ # | Bob | 25 |
584
+
585
+ # When you call the transpose method:
586
+ transposed_table = data_table.transpose()
587
+
588
+ # The transposed data will look like this:
589
+ # [
590
+ # ["Name", "Alice", "Bob"],
591
+ # ["Age", 30, 25]
592
+ # ]
593
+
594
+ # Printing the transposed data:
595
+ for row in transposed_table.rows:
596
+ print ([cell.value for cell in row.cells])
597
+
598
+
514
599
Organizing your scenarios
515
600
-------------------------
516
601
0 commit comments