Behave – Step Implementations

Behave - Step Implementations

In this chapter, we will discuss 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 −

Behave - Step Implementations

The step definition file contains Python functions that define the steps in the feature file. Behave – Step Implementations at the start of the Python functions, it is mandatory to have decorators which begin 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')

Output

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

Behave - Step Implementations

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

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply