-
Notifications
You must be signed in to change notification settings - Fork 0
Cucumber
BDD stands for Behavior Driven Development. This is where you develop based on how the app should behave or at least how you think the customer would like it to behave. With Testing and Cucumber this becomes use coding our test based on these behaviors. Now this should be a workflow from beginning to end of development but in most of the testing world this just becomes a way we code things. Is that ok? IDK but it works and as my dad always said "It is good enough for who it is for."
Below is an example given on the Cucumber Website of Gherkin.
Feature: Guess the word
# The first example has two steps
Scenario: Maker starts a game
When the Maker starts a game
Then the Maker waits for a Breaker to join
# The second example has three steps
Scenario: Breaker joins a game
Given the Maker has started a game with the word "silky"
When the Breaker joins the Maker's game
Then the Breaker must guess a word with 5 characters
Gherkin uses keywords and allows the user to write down a user story in a behavior driven way in order to make the test understandable to everyone else. Below are Keywords and what they are used for
- Feature: Is used to give a high level over view and to tie several scenarios to the same feature and is needed for a Cucumber feature file.
- Rule: Is optional and new to Cucumber. This is a business rule that can be tied to several scenarios. These use Example instead of Scenario.
- Example: Is a Scenario that is tied to a Rule.
Feature: Highlander
Rule: There can be only One
Example: Only One -- More than one alive
Given there are 3 ninjas
And there are more than one ninja alive
When 2 ninjas meet, they will fight
Then one ninja dies (but not me)
And there is one ninja less alive
Example: Only One -- One alive
Given there is only 1 ninja alive
Then he (or she) will live forever ;-)
Rule: There can be Two (in some cases)
Example: Two -- Dead and Reborn as Phoenix
-
Steps
- Given is used to descrip the initial setup sometimes or something that has already happened
- When is an event or action. Possibly done by the user.
- Then is the expected outcome
- And & But are used to extend one of the above steps and split into more then one step if needed.
- Background is used when a given is repeated over a lot of test. You can then put it in a Background and it will run before every Scenario.
- Scenario: is what is used to define a test that is then broken out into the steps
- Scenario Outline: is used when you want to tie a Scenario to a datatable. This is great for something like login where you want to pass in a table of usernames and passwords.
Scenario Outline: Login
Given I am at a login screen
When I enter in <Username> and <password>
Then I should have be able to login
Examples:
| Username | password |
| benweese | password123 |
| johndoe | 123password |
Step Definitions are methods in your java code that are tied to the feature and gherkin by a tag. Before your method you will have a @keyword(Stuff) that will tie to it. It will have to match your gherkin. Intellij will help you create them when you run the feature and it will error with a outline of the java. You can also pass parameters in from the gherkin.
Given I have 3 red balls
@Given("I have {int} red balls")
public void i_have_red_balls(int int1) {
}