Behave – Hooks

Behave - Hooks

Behave Hooks setup and teardown functions are implemented in a file called the environment.py which is within the same directory that contains the steps folder. The connection, configurations, and so on.

TheΒ environment.py functions βˆ’

  • before_feature(context, feature) βˆ’ Executes prior every feature.
  • before_scenario(context, scenario) βˆ’ Executes prior every scenario.
  • before_step(context, step) βˆ’ Executes prior every step.
  • before_tag(context, tag) βˆ’ Executes prior every tag.
  • before_all(context) βˆ’ Executes prior everything.
  • after_feature(context, feature) βˆ’ Executes post every feature.
  • after_scenario(context, scenario) βˆ’ Executes post every scenario.
  • after_step(context, step) βˆ’ Executes post every step.
  • after_tag(context, tag) βˆ’ Executes post every tag.
  • after_all(context) βˆ’ Executes post everything.

The project structure should be as follows βˆ’

Behave - Hooks

Feature File with hooks (Payment. feature)

The feature file with Behave hooks for the Payment. the feature is as follows βˆ’

Feature βˆ’ Payment Process
Scenario βˆ’ Verify transactions
         Given user makes a payment of 100 INR And user makes a payment of 10 Dollar

Feature File with hooks (Payment1.feature)

The setup functions include – browser open, database connection, configurations, and so on. Given below is the feature file with Behave hooks for Payment1.feature βˆ’

Feature βˆ’ Administration Process
Scenario βˆ’ Verify admin transactions
         Given user is on admin screen

Corresponding step Implementation File

The step implementation file is as follows βˆ’

from behave import *
from parse_type import TypeBuilder
parse_amt = TypeBuilder.make_choice(["100", "10"])
register_type(Amt=parse_amt)
parse_curr = TypeBuilder.make_choice(["INR", "Dollar"])
register_type(Curn=parse_curr)
@given("user makes a payment of {n:Amt} {t:Curn}")
def step_payment(context, n, t):
   pass
@given('user is on admin screen')
def step_admin(context):
   pass

Step 4 βˆ’ Hooks in environment.py file

The hooks inΒ the environment.pyΒ file are as follows:

# before all
def before_all(context):
   print('Before all executed')
# before every scenario
def before_scenario(scenario, context):
   print('Before scenario executed')
# after every feature
def after_feature(scenario, context):
   print('After feature executed')
# after all
def after_all(context):
   print('After all executed')

Output

The project structure should be as follows βˆ’

Behave - Hooks

Next Topic – Click Here

This Post Has 3 Comments

Leave a Reply