Behave 步骤中的步骤
我们可以用一个宏步骤代替一个场景中的多个步骤。这有助于我们在步骤定义文件中不重复相同的代码。BDD框架有能力从步骤定义中调用多个步骤。
具有类似步骤的特征文件
具有类似步骤的特征文件如下所示
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
在特征文件中,我们有两个具有类似步骤的Scenario。在Behave中,我们可以在一个步骤中执行一个以上的步骤。这可以在步骤实现文件中的context.execute_steps方法的帮助下完成。
相应的步骤实现文件
上述特征文件的对应步骤实现文件如下
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')
输出
运行特征文件后得到的输出如下,使用的命令是 behave –no-capture -f plain .
继续输出如下 –
输出显示,Scenario Verify的新用户可以通过执行Scenario Verify新用户可以处理付款的步骤来处理付款。