Python behave is a much widely used BDD framework these days. In this blog article, you will get to learn behave BDD framework’s features and how to use it to create automation test scripts. Behave is an open source tool which has 62 contributors who are actively developing new features and fixing the issues. Python Behave is a matured BDD framework.
Implementing readable and business friendly automation testing solution is the need of the hour. When your automation test suites gives provision for non-technical people to collaborate, it will be a value add to your product/project. As an automation testing services company, we get lot of inquiries on implementing business friendly test automation solution of late.
Feature Files
behave is considered to be a clone of Cucumber BDD framework. Most of the features of behave is similar to Cucumber. However, there is a provision for additional flexibility in behave framework. Let’s have a look at them one by one. You can execute feature files in the following ways – by providing feature name, feature directory, at last without providing feature details. When you run behave command without feature path, it will search feature files in features directory by default.
Fixtures
Fixtures aka Hooks are used to call Setup & Cleanup code before and after Test Run, Feature, Scenario, and Tag. You needn’t write a separate method for ‘After’ hooks. Both ‘Before’ and ‘After’ hooks can be implemented inside a single method. Let’s look at the below example.
Feature
@launch.browser Feature: Codoid Website Feature Scenario: Contact Codoid Team Given I am on home page When I submit contact us form Then I should see Thank You page with a message
environment.py
from behave import fixture, use_fixture from selenium import webdriver @fixture def launch_browser(context, timeout=30, **kwargs): context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe') yield context.driver context.driver.quit() def before_tag(context, tag): if tag == "launch.browser": the_fixture = use_fixture(launch_browser, context)
step_def.py
from behave import given, when, then from selenium import webdriver @given('I am on home page') def step_impl(context): context.driver.get("https://codoid.com") assert True is True
Executing Remaining Steps in a Scenario
By default, scenario execution will be stopped if any of the steps is failed. However, you can override this in behave framework using Hooks and Command line.
Hook
from behave.model import Scenario def before_all(context): userdata = context.config.userdata continue_after_failed = userdata.getbool("runner.continue_after_failed_step", False) Scenario.continue_after_failed_step = continue_after_failed
Command Line
behave -D runner.continue_after_failed_step=true features/
Filtering Scenarios and Examples using Tag Value
You can select scenarios and examples using Tag and its value. This feature is helpful when you want to select an example based on the environment value for a tag.
Feature: Scenario Outline: Wow Given an employee "<name>" @use.with_stage=develop Examples: Araxas | name | birthyear | | Alice | 1985 | | Bob | 1975 | @use.with_stage=integration Examples: | name | birthyear | | Charly | 1995 |
In Conclusion
As a test automation services company, we advocate behave BDD framework for creating readable and understandable automation test suite. It is still widely used by the automation community and its contributors are releasing newer versions frequently based on the market need.
Comments(0)