Skip to content

Commit f9728ef

Browse files
committed
Add to README how to make empty example cells return None, and add docstring to test
1 parent 9826dc7 commit f9728ef

File tree

2 files changed

+89
-33
lines changed

2 files changed

+89
-33
lines changed

README.rst

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -565,39 +565,6 @@ Example:
565565
assert datatable[1][1] in ["user1", "user2"]
566566
567567
568-
Rules
569-
-----
570-
571-
In Gherkin, `Rules` allow you to group related scenarios or examples under a shared context.
572-
This is useful when you want to define different conditions or behaviours
573-
for multiple examples that follow a similar structure.
574-
You can use either ``Scenario`` or ``Example`` to define individual cases, as they are aliases and function identically.
575-
576-
Additionally, **tags** applied to a rule will be automatically applied to all the **examples or scenarios**
577-
under that rule, making it easier to organize and filter tests during execution.
578-
579-
Example:
580-
581-
.. code-block:: gherkin
582-
583-
Feature: Rules and examples
584-
585-
@feature_tag
586-
Rule: A rule for valid cases
587-
588-
@rule_tag
589-
Example: Valid case 1
590-
Given I have a valid input
591-
When I process the input
592-
Then the result should be successful
593-
594-
Rule: A rule for invalid cases
595-
Example: Invalid case
596-
Given I have an invalid input
597-
When I process the input
598-
Then the result should be an error
599-
600-
601568
Scenario Outlines with Multiple Example Tables
602569
----------------------------------------------
603570

@@ -663,6 +630,91 @@ only the examples under the "Positive results" table will be executed, and the "
663630
pytest -k "positive"
664631
665632
633+
Handling Empty Example Cells
634+
----------------------------
635+
636+
By default, empty cells in the example tables are interpreted as empty strings ("").
637+
However, there may be cases where it is more appropriate to handle them as ``None``.
638+
In such scenarios, you can use a converter with the ``parsers.re`` parser to define a custom behavior for empty values.
639+
640+
For example, the following code demonstrates how to use a custom converter to return ``None`` when an empty cell is encountered:
641+
642+
.. code-block:: gherkin
643+
644+
# content of empty_example_cells.feature
645+
646+
Feature: Handling empty example cells
647+
Scenario Outline: Using converters for empty cells
648+
Given I am starting lunch
649+
Then there are <start> cucumbers
650+
651+
Examples:
652+
| start |
653+
| |
654+
655+
.. code-block:: python
656+
657+
from pytest_bdd import then, parsers
658+
659+
660+
# Define a converter that returns None for empty strings
661+
def empty_to_none(value):
662+
return None if value.strip() == "" else value
663+
664+
665+
@given("I am starting lunch")
666+
def _():
667+
pass
668+
669+
670+
@then(
671+
parsers.re("there are (?P<start>.*?) cucumbers"),
672+
converters={"start": empty_to_none}
673+
)
674+
def _(start):
675+
# Example assertion to demonstrate the conversion
676+
assert start is None
677+
678+
679+
Here, the `start` cell in the example table is empty.
680+
When the ``parsers.re`` parser is combined with the ``empty_to_none`` converter,
681+
the empty cell will be converted to ``None`` and can be handled accordingly in the step definition.
682+
683+
684+
Rules
685+
-----
686+
687+
In Gherkin, `Rules` allow you to group related scenarios or examples under a shared context.
688+
This is useful when you want to define different conditions or behaviours
689+
for multiple examples that follow a similar structure.
690+
You can use either ``Scenario`` or ``Example`` to define individual cases, as they are aliases and function identically.
691+
692+
Additionally, **tags** applied to a rule will be automatically applied to all the **examples or scenarios**
693+
under that rule, making it easier to organize and filter tests during execution.
694+
695+
Example:
696+
697+
.. code-block:: gherkin
698+
699+
Feature: Rules and examples
700+
701+
@feature_tag
702+
Rule: A rule for valid cases
703+
704+
@rule_tag
705+
Example: Valid case 1
706+
Given I have a valid input
707+
When I process the input
708+
Then the result should be successful
709+
710+
Rule: A rule for invalid cases
711+
712+
Example: Invalid case
713+
Given I have an invalid input
714+
When I process the input
715+
Then the result should be an error
716+
717+
666718
Datatables
667719
----------
668720

tests/feature/test_outline_empty_values.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def test_outline():
6565

6666

6767
def test_scenario_with_empty_example_values_none_transformer(pytester):
68+
"""
69+
Checks that `parsers.re` can transform empty values to None with a converter.
70+
`parsers.parse` and `parsers.cfparse` won't work out of the box this way as they will fail to match the steps.
71+
"""
6872
pytester.makefile(
6973
".feature",
7074
outline=textwrap.dedent(

0 commit comments

Comments
 (0)