Behave - Step Implementations


The steps of a Scenario in the feature file in Behave should have implementation logic written in Python. This is known as the implementation/step definition file (.py extension) and should be present within the steps directory.

All the necessary imports are present in this file. The steps directory should be a part of the features directory.

The following screen will appear on your computer −

Step Implementations

The step definition file contains Python functions which define the steps in the feature file. At the start of the Python functions, it is mandatory to have decorators which begins with @given, @when, and so on. These decorators compare and match with the Given, Then, When, and other steps in the feature file.

Feature File

The feature file is as follows −

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

Corresponding Step Implementation File

The corresponding step implementation file looks like the one mentioned below −

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 −

Corresponding Step Implementation File

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