This is the first tutorial in Python Behave BDD series!
What you will Learn :
- What is BDD (Behavior driven development) ?
- Gherkin syntax
- E2E shopping cart Gherkin syntax
- Scenario keyword
- Feature keyword
- Linking Gherkin with python
- Behave
- Steps
- Step Definition
What is BDD and Gherkin syntax
Let us first understand what is BDD (Behavior driven development).
Look at below test case. We have described our test case in the form of a ‘behavior’
So our automation test case will check whether ‘we can search for a course on a typical website’.
However, look at the below test case that was built using pure python syntax. To a client/end-user, the syntax looks to be slightly technical and is not so easy to understand
So by looking at above, it is difficult for the client to understand what exactly you have automated, what test case feature has been automated. They can only guess what this test case might be doing. So the business scenarios are missing in above script. So we cannot say for sure what the above test case exactly does.
Instead, our test case can be described in this behavior driven style (see below)
The above syntax is called as ‘Gherkin’ syntax and our client can easily understand it. The syntax closely matches with the English language (having ‘Given’, ‘When’, Then’ etc keywords). Most lines in a Gherkin syntax start with one of the keywords (Given, When etc).
So you can understand the test case syntax as below:
Given explains what you have
When you perform some action
And you perform additional action
Then what is the result or outcome of that action
Below is another example of a test case in Gherkin syntax.
Scenario: All shopping done
Given I am out shopping
And I have eggs
And I have milk
And I have butter
When I check my shopping list
Then I should not have anything left
So you can describe your test scenario using Gherkin syntax and you can link these lines with actual python code.
E2E shopping cart Gherkin syntax
Consider below end to end test case:
Let us try to convert above steps into Gherkin syntax. So, below is the business representation in BDD Behave world which is self-explanatory.
Recall that ‘And’ keyword is used to concatenate additional actions.
Scenario: E2E automation
Given I launch an e-commerce website
When I enter ‘Lenovo IdeaCentre 600 All-in-One PC’ in search field
And I search the item
And I add item to shopping cart
And I go to shopping cart
And I enter 2 in quantity field
And I update my shopping cart
Then ‘Total’ amount should be $1000
Scenario keyword
As you must have noticed in above test cases, in addition to other keywords, we also used ‘Scenario’ keyword. A ‘Scenario’ describes intended behavior of the test case. In other words, a ‘Scenario’ describes what, not how.
Consider below test case.
The ‘Scenario’ is describing the what viz what the order details would show up. It does not describe how the order details will be seen
Feature keyword
‘Feature’ represents a Test Suite. A test suite can have multiple test cases. Each scenario represents one test case. So if you consider below example, we have a ‘Feature’ that contains 2 test cases or 2 scenarios
The first scenario talks about new user registration and the second test case about existing user registration.
Linking Gherkin with python
Let us look at below test case again. We will ultimately be linking all our Gherkin syntax with python code, as shown below.
Scenario: E2E automation
Given I launch an e-commerce website –>linked to
<some python code to launch website> .get(‘https://demo.nopcommerce.com/’)
When I enter ‘Lenovo IdeaCentre 600 All-in-One PC’ in search field –>linked to
<some python code to type text in the search box> .sendkeys(‘Lenovo IdeaCentre 600 All-in-One PC’)
And I search the item –>linked to
<some python code to click the search button> .click()
And I add item to shopping cart –>linked to
<some python code to add item to cart> (‘.product-box-add-to-cart-button’).click()
And I go to shopping cart –>linked to
<some python code to click the shopping cart> (‘.cart-label’).click()
And I enter 2 in quantity field –>linked to
<some python code to type numeric 2 in qty field> .sendkeys(‘2’)
And I update my shopping cart –>linked to
<some python code to click and update the cart> (“[value=’Update shopping cart’]”).click()
Then ‘Total’ amount should be $1000 –> linked to
<some python code to validate the amount> (‘.product-subtotal’).assertEquals(‘$1,000.00’)
However, we will not share the python code with end user, we will only share the BDD (behavior driven development) Gherkin syntax with client.
Behave
As we have seen above, we describe the desired behavior in a test case and based upon this behavior, we drive our development. By development, we mean writing the actual python code and linking it to behavior.
That’s the reason we call it as ‘Behavior Driven Development’
Behave is one of the frameworks supports this Behavior Driven Development.
Steps
Now, what is a step? Each line of a scenario is a step (Give/When/Then…….).
See below. The lines 4-7, 10-11 are steps:
Step Definition
These steps are defined by writing an actual python code having step definitions. So, in ‘Behavior Driven Development’, we write our python code inside ‘Step Definition’ file. The link-up depends upon the programming language you are using. Different programming languages have different syntax to link.
Below is a typical step definition file. These files are python files having .py extension
So this was an introduction of Python Behave BDD.
Thank you for reading!