Skip to content

Commit

Permalink
Merge pull request #713 from jsa34/asterisk-steps
Browse files Browse the repository at this point in the history
Add test to use asterisks to continue step type instead of And or But
  • Loading branch information
youtux authored Oct 6, 2024
2 parents b3956b3 + 15b83ee commit 626df4c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Unreleased
----------
- Update documentation to clarify that `--gherkin-terminal-reporter` needs to be used with `-v` or `-vv`.
- Drop compatibility with pytest < 7.0.0.
- Continuation of steps using asterisks instead of And/But supported.

8.0.0b1
----------
Expand Down
49 changes: 49 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,55 @@ default author.
And there's an article
Using Asterisks in Place of Keywords
------------------------------------

To avoid redundancy or unnecessary repetition of keywords
such as "And" or "But" in Gherkin scenarios,
you can use an asterisk (*) as a shorthand.
The asterisk acts as a wildcard, allowing for the same functionality
without repeating the keyword explicitly.
It improves readability by making the steps easier to follow,
especially when the specific keyword does not add value to the scenario's clarity.

The asterisk will work the same as other step keywords - Given, When, Then - it follows.

For example:

.. code-block:: gherkin
Feature: Resource owner
Scenario: I'm the author
Given I'm an author
* I have an article
* I have a pen
.. code-block:: python
from pytest_bdd import given
@given("I'm an author")
def _():
pass
@given("I have an article")
def _():
pass
@given("I have a pen")
def _():
pass
In the scenario above, the asterisk (*) replaces the And or Given keywords.
This allows for cleaner scenarios while still linking related steps together in the context of the scenario.

This approach is particularly useful when you have a series of steps
that do not require explicitly stating whether they are part of the "Given", "When", or "Then" context
but are part of the logical flow of the scenario.


Step arguments
--------------

Expand Down
63 changes: 63 additions & 0 deletions tests/steps/test_keyword.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import textwrap


def test_asterisk_keyword(pytester):
pytester.makefile(
".feature",
asterisk=textwrap.dedent(
"""\
Feature: Step continuation
Scenario: Asterisk steps
Given I am out shopping
* I have eggs
* I have milk
* I have butter
When I check my list
Then I don't need anything
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
import pytest
from pytest_bdd import given, when, then, scenario
@scenario("asterisk.feature", "Asterisk steps")
def test_asterisk_steps():
pass
@given("I am out shopping")
def _():
pass
@given("I have eggs")
def _():
pass
@given("I have milk")
def _():
pass
@given("I have butter")
def _():
pass
@when("I check my list")
def _():
pass
@then("I don't need anything")
def _():
pass
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=1)

0 comments on commit 626df4c

Please sign in to comment.