Python Behave Tutorial: A Comprehensive Guide to Behavior-Driven Development (BDD)
In this tutorial, we'll explain each and every aspect of Behavior Driven Development. This BDD Python tutorial will help you create and design your automation tests and framework.
Table of Content
1. What is BDD?2. What is Gherkin Language in BDD?
3. What is Behave in Python?
4. Installing and Setup Behave
5. Project Structure for Behave Python (BDD)
6. Feature Files, Step Functions and Other Files in Behave
- Feature File
- Step Functions
- Step Parameters
- Passing String Param from Feature File
- Passing Integer Param from Feature File
- environment.py file
- behave.ini file
7. Run Feature files in Behave
- Run a single feature file
- Run multiple feature files
- Generate JUNIT xml reports with behave
- Run feature files with Python suite file
Upcoming Tutorials:
8. API Automation Testing with BDD Python (Behave)
9. Selenium Automation with BDD Python (Behave)
1. What is BDD?
- Write the test before the development code is written, which should actually fail.
- Then make that failing test pass after the development code is written.
2. What is Gherkin Language in BDD?
- Given
- When
- Then
- And
- Feature
- Background
- Scenario
- Scenario Outline
Feature: Add product to cart Scenario: Login and click summer dresses When I open amazon website And I login with username '[email protected]' and password 'Test@123' Then I hover on women menu item and click summer dresses
- Feature gives a high-level description of the software feature.
- Given is pre-condition.
- When is some action.
- Then is the expected outcome.
- And is used to have additional steps along with Given, When, or Then statements.
3. What is Behave in Python?
- Feature Files like., Login.feature, Product.feature
- Step Definition File like., steps.py
- behave.ini or setup.cfg
- environment.py
- We can create as many feature files as we want or need, say one feature file for one feature under test.
- But one step definition and one behave.ini file.
- The step definition file is the file where all the code is written and mapped to the steps/lines written in the feature file.
- And behave.ini is basically a configuration file for the BDD project.
- And the environment file is for integrating some hooks to our step implementations like before_all, before_feature, after_feature, etc.
- We'll learn more about these files as we progress through this tutorial.
4. Install Behave
4.1. Using the pip command
- For Windows - pip install behave
- For Linux - the pip3 install behave
4.2. Using GitHub Repo
- You need to - git clone https://github.com/behave/behave
- And then in the root of this cloned directory, execute - python setup.py install
4.3. Download and Install PyCharm IDE
5. Project Structure for Behave (BDD)
- Create a new Python project in PyCharm.
- And then create a features directory inside it. All the .feature files will be created under this directory.
- Create a steps directory inside the features directory. The step definition file which is steps.py will be created in this steps directory.
+--features/ | +--steps/ # -- Steps directory | | +-- *.py # -- Step implementation or use step-library python files. | +-- *.feature # -- Feature files.
+-- features/ | +-- steps/ | | +-- website_steps.py | | +-- utils.py | | | +-- environment.py # -- Environment file with behave hooks, etc. | +-- signup.feature | +-- login.feature | +-- account_details.feature
6. Feature Files and Step Functions in Behave
6.1. How to Write a Feature File
- Feature: List of scenarios.
- Background: List of steps run before each of the scenarios
- Scenario: Business rule through the list of steps with arguments.
- Given: Some precondition step
- When: Some key actions
- Then: To observe outcomes or validation
- And, But: To enumerate more Given, When, Then steps
- Scenario Outline: List of steps for data-driven as an Examples and <placeholder>
- Given: Some precondition step with <name>
- When: Some key actions with <value>
- Then: To observe outcomes or validation as <status>
- Examples: Container for s table
| name | value | status |
| name1 | 5 | success |
| name2 | 7 | Fail |- | (Data Tables)
- """ (Doc Strings)
- @ (Tags/Labels): To group Scenarios
- < > (placeholder)
- ## (Comments)
- "" String
Sample Feature Definition Template:
#Author: vb Feature: Title of your feature I want to use this template for my feature file Scenario: Title of your scenario Given I want to write a step with precondition And some other precondition When I complete action And some other action And yet another action Then I validate the outcomes And check more outcomes Scenario Outline: Title of your scenario outline Given I want to write a step with <name> When I check for the <value> in step Then I verify the <status> in step Examples: | name | value | status | | name1 | 5 | success | | name2 | 7 | Fail |
#Author: vb Feature: Login to portal # Setup steps for all scenarios of this feature Background: Given inputs for this feature Given xyz website and it's DB is up # Test Case 1 Scenario: Test Login Functionality When I open xyz website And I login with username '[email protected]' and password 'abc@123' Then I verify that I successfully logged in And I log out
6.2. Step Functions
Project Path: MyProject/features/steps/TestSteps.py
from behave import * @given("xyz website and it's DB is up") def step_impl(context): raise NotImplementedError(u'STEP: Given xyz website and it\'s DB is up') @when("I open xyz website") def step_impl(context): raise NotImplementedError(u'STEP: When I open xyz website') @then("I verify that I successfully logged in") def step_impl(context): raise NotImplementedError(u'STEP: Then I verify that I successfully logged in') @step("I log out") def step_impl(context): raise NotImplementedError(u'STEP: And I log out')
- Importing Behave: You must have noticed that the first line in the steps file is to import behave module.
- Annotations: You can also observe that every step implementation function has an annotation like., @given, @when, @then, and @step. These annotations are mapping these functions to the corresponding steps in feature files.
- The context in Behave: Context is a very important feature in the step implementation file. If you want to share your function variables with other functions then you have to declare them with context. Like.,
context.iam_token = Token.get_iam_token()
If you have declared this iam_token variable in first step_impl function
then it would also be available to all the other step_impl functions.
6.3. Step Parameters
For example., if you have written a step for clicking on a link as "I click on Home". And if you have multiple links then you have to create as many steps as links. But if you parameterize this step like "I click on 'link' ", 'link' is a variable here. So, now we'll pass the variable value from steps, and in the steps file we write our logic to click on the link which is requested by the feature file.
6.3.1 Passing String Parameter from Feature File
Links.feature
Scenario: Test menu links When I click on 'Home' link And I click on 'Dress' link
Steps.py
@given("I click on '{link_text}' link") def step_impl(context, link_text): # Call Click link function click_link(link_text)
6.3.2. Passing Integer Parameter from Feature file
SignUp.feature
Scenario: Test Sign-UP form Given Age of the employee is '26'
Steps.py
@given("Age of the employee is '{age:d}'") def step_impl(context, age): # Enter age enter_value(age)
6.4. Environment File in Behave
Sample environment.py
import logging def before_all(context): print("Executing before all") def before_feature(context, feature): print("Before feature\n") # Create logger context.logger = logging.getLogger('automation_tests') context.logger.setLevel(logging.DEBUG) def before_scenario(context, scenario): print("User data:", context.config.userdata) def after_scenario(context, scenario): print("scenario status" + scenario.status) context.browser.quit() def after_feature(context, feature): print("\nAfter Feature") def after_all(context): print("Executing after all")
6.5. behave.ini
Sample behave.ini:
[behave] stderr_capture=False stdout_capture=False log_capture=False show_timings=True verbose=True color =True
7. Run Your (Test) Feature files in Behave Project
behave --help
i. Run a Single Feature file:
You can run a single feature file with the following command,
behave features\Login.feature
ii. Run Multiple Feature files:
And run all the feature files by giving the features directory path to behave command,
behave features\
iii. Generate JUNIT xml Test Report:
You can also generate the JUnit test report in XML format by using --junit parameter with behave.
behave features\ --junit
Sample Suite.py file
#!/usr/bin/python # -*- coding: utf-8 -*- import sys from behave import __main__ as runner_with_options if __name__ == '__main__': sys.stdout.flush() # Join Feature Files featureFileFolder = '../tests/' # Runner Option set to junit commonRunnerOptions = ' --no-capture --junit --junit-directory ../test-results/e2e ' # Complete Suite fullRunnerOptions = commonRunnerOptions + featureFileFolder var = runner_with_options.main(fullRunnerOptions)
I came across your blog post on Austin metal roofing, and I must say it's an excellent resource for homeowners like myself. Metal roofing is gaining popularity in Austin due to its numerous benefits, and your article effectively highlights why it's a great choice. The extreme weather conditions we experience in Austin, including scorching summers and occasional storms, make the durability and resilience of metal roofing highly desirable. Your insights on how metal roofs can withstand these challenges and provide long-lasting protection were truly informative.
ReplyDeleteRegards,
Alpha Team Roofing
Share Personal Experience: Relate a relevant personal experience related to the blog post's topic to enhance the discussion.Digital marketing IT Center
ReplyDelete"Exceptional tutorial! This comprehensive guide to Python Behave and Behavior-Driven Development is a game-changer. Clear explanations, practical examples, and step-by-step approach make it an indispensable resource for developers. Outstanding work!"
ReplyDelete"Incredible resource! This Python Behave tutorial is a gem for developers diving into Behavior-Driven Development. Comprehensive coverage, real-world examples, and a user-friendly approach make it an essential guide for mastering BDD. Highly recommended!"
ReplyDelete