Behave – Step Functions

Behave - Step Functions

Behave Step functions are created in the Python files which exist within the steps directory. Every Python file (having extension as .py) inside that directory gets imported to get the step implementations.

The step implementations must begin with the import, by using the command mentioned below −

from behave import *

This will import multiple decorators described in Behave to help us to locate our step functions. The decorators like the given, when, then, and so on accepts one string argument.

For example, consider the code given herewith −

@given('user is on admin screen')
def step_impl(context):
      pass

The above code shall match the Given step of the below feature file, which is as follows −

Feature − Admin Module
Scenario − Admin verification
      Given user is on admin screen

The steps starting with And/But in the keyword.

For example, consider the feature file given below −

Feature − Admin Module
Scenario − Admin verification
      Given user is on admin screen
       And user is on history screen
       Then user should be able to see admin name
         But user should not able to check history

The And step shall be the keyword of Behave Step functions.

If there are more than one And/But steps consecutively, they would inherit the keyword of non And or But keyword.

The step function having the step decorator shall have a minimum of one parameter. Other parameters come from step parameters (if required).

For example, refer to the step function as per the step parameter.

@given('user is on admin screen')
def step_impl(context):
      pass

Project Structure

The project structure for the feature is as follows −

Behave - Step Functions

Next Topic – Click Here

This Post Has One Comment

Leave a Reply