Skip to content

Commit 430501b

Browse files
committed
Add provisional documentation for data_table fixture
Fix documentation to no longer allow multiline steps without using triple quotes
1 parent ae721f9 commit 430501b

File tree

1 file changed

+88
-3
lines changed

1 file changed

+88
-3
lines changed

README.rst

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,20 +384,21 @@ Multiline steps
384384

385385
As Gherkin, pytest-bdd supports multiline steps
386386
(a.k.a. `Doc Strings <https://cucumber.io/docs/gherkin/reference/#doc-strings>`_).
387-
But in much cleaner and powerful way:
388387

389388
.. code-block:: gherkin
390389
391390
Feature: Multiline steps
392391
Scenario: Multiline step using sub indentation
393392
Given I have a step with:
393+
"""
394394
Some
395395
Extra
396396
Lines
397+
"""
397398
Then the text should be parsed with correct indentation
398399
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.
401402
In the example above, the Given step name will be:
402403

403404
.. code-block:: python
@@ -511,6 +512,90 @@ Example:
511512
assert cucumbers["start"] - cucumbers["eat"] == left
512513
513514
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+
514599
Organizing your scenarios
515600
-------------------------
516601

0 commit comments

Comments
 (0)