Tutorial 6 – Create feature and Step definition using Behave
Welcome to the 6th article in Behave BDD series!
What you will Learn :
Create a feature file
Run the feature file
Create Step Definition file
Re-run the feature file
Logging standard output for the tests that pass
behave --help
Create a feature file
We will now create a feature file in PyCharm IDE. So lets launch our IDE and create Search.feature file under the pythonProject1. You can refer the previous tutorial on how to create a feature file. The filename should describe some feature (example: login, search, addtocart etc). The file name must have .feature suffix
Now copy the below feature:
Feature: Way2Automation search
Scenario: Search course
Given Search field should be present on the Way2Automation website
When I search for a course 'cucumber BDD for Selenium' having price 1000
Then Course 'Cucumber BDD for Selenium & Appium with Live Projects' should be displayed
Paste it inside the feature file
Note#1: ‘Given’ keyword defines some pre-condition
Note#2: Carefully see lines#4,5,6 above. Do not add colon after ‘Given/When/Then’ keywords. Thus is wrong. You should use
Note#3: ‘When’ step defines some action to interact with application
Note#4: ‘Then’ step defines an outcome, some verification, some expectation
Note#5: ‘And’ represents the preceding keyword (‘Given’, ‘When’, ‘Then’)
Note#6: Since the name of the course is a string, it should be in quotes. The price is numeric and hence without quotes
So this was our first feature file having one scenario. Likewise, you can write any number of scenarios for search feature.
Run the feature file
Let us now run the feature file that we have created above. To do that, click ‘Terminal’ that you see at the bottom of PyCharm window, see below
When you click Terminal, the virtual environment comes up
Run the dir command, it would show us the files inside the current dir
Let us execute behave Search.feature command to execute our feature file. Notice that we get an error saying that there is no ‘steps’ directory inside the curent folder pythonProject1
So let us create a directory inside our project
Mention the name as steps
Hit Enter
Now let us re-execute our behave command, see below
See above. The scenarios failed. There is a recommendation to implement the step defintions for each Give/When/Then step.
So basically, the feature file currently is in plain English language. We have not yet implemented the 3 steps in our feature file and the console has suggested us 3 python methods (code snippets) that need to be implemented.
Now see below. Select all the methods and copy them
Create Step Definition file
Below depicts BDD framework design. So, in a step definition file, we map the Give/When/Then steps using @ annotations
Now, right click ‘steps’ folder > New > Python File
Mention some name.py to create a step definiton file
Hit Enter
Paste the methods in this file
Next, remove all the exceptions (lines 3, 8, 14 that you see above). So we end up with below
Next, import the gherkin keywords from behave library
Logging standard output for the tests that pass
We still see 3 errors. To resolve these, write print statements for each of the 3 methods, see below
Save the file.
Next, go to the terminal and re-run the behave command to execute the feature file. Notice below that this time the feature got passed
If you notice above, we don’t see the output of print commands that we wrote in our step def file (lines 5, 9, 13) see below
The reason is that, by default, behave will only put the error messages on the console. By default, it does not log the messages for the test cases that pass.
To log the messages from passed scenarios, just add –no-capture in addition to behave command. Notice below that we now see the output of print statements
Let us remove ‘website’ from the below @given step in the step definition file
So now, our @given step is as shown below
Save the file and run the feature file. Notice below that the feature failed this time. It is asking us to implement the mising method
So, in essence, what we have done is that (see below), the ‘Given’ step in feature file is mapped to @given in step definition file. Also, the exact step name written in feature file "Search field should be present on the Way2Automation website" is mapped to the step of @given method in the step def file. Hence, the step name in step def file CANNOT be changed.
Let us again add ‘website’ to our step def file
Save the file. When we now run the feature, the scenarios pass (like we saw earlier)
Also, right now, the name of all the 3 methods is same viz step_impl (lines 4, 8, 12)
Let us change the method names (lines 4, 8, 12)
Save and run the feature file. Notice below that it is pass
So what we conclude from here is that, we can change the method name to any other desired name.
behave --help
This command will show you all the arguments that you can use with behave command, see below
Thank you for reading!