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
STAY IN THE LOOP
Subscribe to our free newsletter.
Software testing frequently encounters sporadic problems, such network outages. Even if the application being tested is working properly, these problems can cause automated test scenarios to fail. We have created a new retry functionality that automatically retries failed BDD scenarios in order to solve this issue. In this blog post […]
This article addresses the main differences between Behavior Driven Development (BDD) and Test-Driven Development (TDD) with real-world examples and code snippets. It is a common misconception that Behavior Driven Development is an extension of Test-Driven Development and this article explains how it is different and how both can be used […]