Behave – Steps in a Step

Behave - Steps in a Step

Behave – Steps in a Step. We can substitute multiple steps in a Scenario with one macro step. This helps us not to repeat the same code in the step definition file. A BDD framework has the capability to invoke multiple steps from the step definition.

Feature File with Similar Steps

The feature file with similar steps is as follows βˆ’

Feature βˆ’ Payment Module
   Scenario βˆ’ Verify message after payment
      Given User is on payment screen
      When User enters payment details
      And User completes payment
      Then User should get success message
   Scenario βˆ’ Verify new users can process payment
      Given User keys in payment info and submits
      Then success message should get displayed

In the feature file, we have two Scenarios with similar steps. In Behave Steps in a Step, we can execute more than one step in a single step. This can be done with the help of context.execute_steps method in the step implementation file.

Corresponding Step Implementation File

The corresponding step implementation file for the above-mentioned feature file is as follows βˆ’

from behave import *
@given('User is on payment screen')
def is_on_payment_screen(context):
   print('User is on payment screen')
@when('User enters payment details')
def enters_payment_details(context):
   print('When User enters payment details')
@when('User completes payment')
def completes_payment(context):
   print('When User completes payment')
@then('User should get success message')
def get_success_message(context):
   print('Then User should get success message')
   @given('User keys in payment info and submits')
def payment_info_and_submits(context):
#passing steps within steps with context.execute_steps
   context.execute_steps(u"""
      Given User is on payment screen
      When User enters payment details
      And User completes payment
      """)
@then('success message should get displayed')
def success_message(context):
   print('Then success message should get displayed')

Output

The output used isΒ behave –no-capture -f plain.

Behave - Steps in a Step

The continued output is as follows βˆ’

Behave - Steps in a Step

The output shows that the new users of Scenario Verify can process the payment by having the steps executed from the Scenario Verify new users can process payment.

Next Topic – Click Here

This Post Has One Comment

Leave a Reply