Behave - Introduction

Behave is a tool used for Behaviour driven development (BDD) in Python programming language. In an Agile development framework, BDD creates a culture where testers, developers, business analysts, and other stakeholders of the project can contribute towards the software development.

In short, both technical and non-technical individuals have a role to play towards the overall project. Behave has tests developed in plain text with the implementation logic in Python.

The BDD format begins with the description of the characteristics of the software similar to a story.

It then continues with the development and carries out the following tasks −

  • Developing a failing test case for characteristics.

  • Implement the logic for a test to pass.

  • Code refactor to fulfil the project guidelines.

There are numerous libraries for BDD like the Mocha which supports JavaScript, Cucumber which supports Java/Ruby, and Behave which supports Python, and so on.

In this tutorial, we shall discuss in detail about Behave.

Let us see a basic structure of a BDD. It mainly consists of the feature file, the step definition file, and so on.

Feature File

The feature file in Behave can be as follows −

Feature − Verify book name added in Library.
Scenario − Verify Book name.
Given − Book details.
Then − Verify book name.

Corresponding step definition file

Following is the corresponding definition file in Behave tool −

from behave import *
@given('Book details')
def impl_bk(context):
   print('Book details entered')
@then('Verify book name')
def impl_bk(context):
   print('Verify book name')


The output obtained after running the feature file is as follows −

Behaviour Driven Development

The output shows the Feature and Scenario names, along with the test results, and the duration of the respective test execution.