Step definitions (Given
, When
, Then
) are the glue between features written in Gherkin and the actual tests implementation.
Cucumber supports two types of expressions:
- Cucumber expressions
- Regular expressions
See also the reference documentation.
The following Gherkin step:
Given I have 42 cucumbers in my belly
Can be implemented with following Cucumber Expression in Groovy:
Given("I have {int} cucumbers in my belly"){ int cucumberCount ->
// Do something
}
The following Gherkin step:
Given I have 42 cucumbers in my belly
Can be implemented with following Regular Expression in Groovy:
Given(~/'^I have (\d+) cucumbers in my belly$'/){ int cucumberCount ->
// Do something
}
To implement multiple gherkin steps in a single cucumber step:
Given([~/^I have (\d+) cuke in my belly/, ~/^I have (\d+) cukes in my belly/] as Pattern[]) { int cukes ->
// Do something
}