Behave 简明教程

Behave - Steps in a Step

我们可以在一个情景中用一个宏步骤替换多个步骤。这有助于我们在步骤定义文件中不重复相同的代码。BDD 框架有能力从步骤定义调用多个步骤。

Feature File with Similar Steps

具有类似步骤的特性文件如下所示 -

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 方法完成。

Corresponding Step Implementation File

上面提到的特性文件对应的步骤实现文件如下 -

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

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

no capture

后续输出如下所示 −

scenario verify

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