I want to share information between step frequently. For this, I need to data available globally.
Lets see the scenario.
Feature: Display email
Scenario: Email should display when I logged in
Given I create a random email When I login with the random email Then I should see the email in the page
I want to use random email variable created Given step on the When and Then steps. A random email is generated on the first step.
How can I use that on the next step to login?
I can store the value using "context".
context.<variable name> = <value>
Lets see the example.
.py
from behave import given, when, then @given("I find recent order from database") def find_order_from_db(context): print("Finding an order from the database...") context.order_num = '112233' print(f"Found an order. Order number: {context.order_num}") @when("I issue a refund for the order") def issue_refund(context): print(f"Trying to issue a refund for order number: {context.order_num}") @then("Payment should get processed for the user") def payment_should_process(context): print("Payment successfully processed") print(f"Payment is for refund of order number: {context.order_num}") @when("I issue a refund on the same order") def issue_repeat_refund(context): print(f"Trying to issue refund on same order: {context.order_num}") @then("The refund should fail to process") def refund_fails(context): print(f"The refund for order {context.order_num} should fail")
Result