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

在功能文件中,我们有两个步骤相似的场景。在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')

输出

运行功能文件后获得的输出如下所示,使用的命令是beeve --no-capture -f plain

无捕获

连续输出如下 -

场景验证

输出显示场景验证的新用户可以通过执行场景验证新用户可以处理付款的步骤来处理付款。