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 - payment.feature

The feature file with the 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 Scenario with similar steps. In Behave, 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 - stepImpPayment.py

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 obtained after running the feature file is given below and the command used is behave --no-capture -f plain.

(myenv) D:\behave\myenv\pythonProject>behave --no-capture -f plain
USING RUNNER: behave.runner:Runner
Feature: Payment Module

  Scenario: Verify message after payment
User is on payment screen
    Given User is on payment screen ... passed in 0.001s
When User enters payment details
    When User enters payment details ... passed in 0.000s
When User completes payment
    And User completes payment ... passed in 0.000s
Then User should get success message
    Then User should get success message ... passed in 0.000s

  Scenario: Verify new users can process payment
User is on payment screen
When User enters payment details
When User completes payment
    Given User keys in payment info and submits ... passed in 0.001s
Then success message should get displayed
    Then success message should get displayed ... passed in 0.000s

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped
Took 0min 0.002s

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.

Advertisements