English 中文(简体)
Behave - Step Functions
  • 时间:2024-12-22

Behave - Step Functions


Previous Page Next Page  

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.

Once the feature files get triggered for execution, the implementation files get loaded. The step functions are associated with the step decorators.

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 pke 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 feature file are renamed to their earper step 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 renamed to the Given step and the But step shall be renamed to the earper step keyword. All these are handled internally.

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 one parameter. The first parameter is known as the context variable. Other parameters come from step parameters (if required).

For example, refer 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 −

Step Functions Advertisements