Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty example table entries with converter to None documentation #747

Merged
merged 5 commits into from
Dec 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 85 additions & 33 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -565,39 +565,6 @@ Example:
assert datatable[1][1] in ["user1", "user2"]


Rules
-----

In Gherkin, `Rules` allow you to group related scenarios or examples under a shared context.
This is useful when you want to define different conditions or behaviours
for multiple examples that follow a similar structure.
You can use either ``Scenario`` or ``Example`` to define individual cases, as they are aliases and function identically.

Additionally, **tags** applied to a rule will be automatically applied to all the **examples or scenarios**
under that rule, making it easier to organize and filter tests during execution.

Example:

.. code-block:: gherkin

Feature: Rules and examples

@feature_tag
Rule: A rule for valid cases

@rule_tag
Example: Valid case 1
Given I have a valid input
When I process the input
Then the result should be successful

Rule: A rule for invalid cases
Example: Invalid case
Given I have an invalid input
When I process the input
Then the result should be an error


Scenario Outlines with Multiple Example Tables
----------------------------------------------

Expand Down Expand Up @@ -663,6 +630,91 @@ only the examples under the "Positive results" table will be executed, and the "
pytest -k "positive"


Handling Empty Example Cells
----------------------------

By default, empty cells in the example tables are interpreted as empty strings ("").
However, there may be cases where it is more appropriate to handle them as ``None``.
In such scenarios, you can use a converter with the ``parsers.re`` parser to define a custom behavior for empty values.

For example, the following code demonstrates how to use a custom converter to return ``None`` when an empty cell is encountered:

.. code-block:: gherkin

# content of empty_example_cells.feature

Feature: Handling empty example cells
Scenario Outline: Using converters for empty cells
Given I am starting lunch
Then there are <start> cucumbers

Examples:
| start |
| |

.. code-block:: python

from pytest_bdd import then, parsers


# Define a converter that returns None for empty strings
def empty_to_none(value):
return None if value.strip() == "" else value


@given("I am starting lunch")
def _():
pass


@then(
parsers.re("there are (?P<start>.*?) cucumbers"),
converters={"start": empty_to_none}
)
def _(start):
# Example assertion to demonstrate the conversion
assert start is None


Here, the `start` cell in the example table is empty.
When the ``parsers.re`` parser is combined with the ``empty_to_none`` converter,
the empty cell will be converted to ``None`` and can be handled accordingly in the step definition.


Rules
-----

In Gherkin, `Rules` allow you to group related scenarios or examples under a shared context.
This is useful when you want to define different conditions or behaviours
for multiple examples that follow a similar structure.
You can use either ``Scenario`` or ``Example`` to define individual cases, as they are aliases and function identically.

Additionally, **tags** applied to a rule will be automatically applied to all the **examples or scenarios**
under that rule, making it easier to organize and filter tests during execution.

Example:

.. code-block:: gherkin

Feature: Rules and examples

@feature_tag
Rule: A rule for valid cases

@rule_tag
Example: Valid case 1
Given I have a valid input
When I process the input
Then the result should be successful

Rule: A rule for invalid cases

Example: Invalid case
Given I have an invalid input
When I process the input
Then the result should be an error


Datatables
----------

Expand Down
Loading