Given, When, and Then are the three essential components of behavior-driven development (BDD), a software development methodology that focuses on defining the behavior of software through examples in plain language.
Here’s a brief description of each component:
- Given: This represents the preconditions or initial state of the system before the behavior being described occurs. It sets up the context for the behavior.
- When: This represents the specific action or event that triggers the behavior being described. It describes the action that changes the state of the system.
- Then: This represents the expected outcome or result of the behavior being described. It describes what the system should do or what should happen after the action described in the “When” section.
Here are few examples for Given, When and Then:
Given (Pre-conditions for your test like opening a website, creating or deleting data etc…)
- Given that the user is on the home page of an online shopping website (In this you would typically have a step definition that opens the shopping website and clicks the home page)
- Given that the user has opened the restaurant booking application on their mobile device (In this, you would typically have the mobile app opened)
- Given user “John” is already registered (In this, you would typically create the user “John” in database)
When (The actual actions in your test like filliing website info and submitting, sending an API)
- When the user searches for a specific product and adds it to their shopping cart (In this, you would typically do your action in other words your actual action for your test. So in this case you would add the product to shopping cart)
- When the user selects a specific date and time, and chooses a preferred restaurant from the list. (In this, you would typically select a date and time in your step def)
- When the user types in a post or message and clicks on the “Post” button. (In this, you would navigate and click on post button)
Then (The actual verification i.e. Asserts for your test like checking form submitted successfully, or checking API response)
- Then the product should be added to the user’s shopping cart and the cart total should be updated accordingly
- Then the user should receive a confirmation message with the details of their booking, including the restaurant name, date, and time.
- Then the user’s post or message should be visible to their contacts in the newsfeed or inbox.
And (is used to combine two or more conditions within a single step)
- Given the user “John” is already registered
And opens the website and logs in
- When the user enters all mandatory fields in the form
And enters optional address field and submits
- Then the API returns “userName”, “Address” information
And httpStatus as 200