Behave 背景

Behave 背景

加入背景是为了有一组步骤。它接近于一个场景。我们可以用背景为多个场景添加一个背景。它在一个功能的每个场景之前运行,但在钩子之前的执行之后。

背景通常用于执行前提条件,如登录场景或数据库连接,等等。

可以添加一个背景描述,以提高人类的可读性。它只能在特征文件中出现一次,并且必须在场景或场景大纲之前声明。

背景不应该被用来创建一个复杂的状态(只有在无法避免的情况下)。这个部分应该是简短而真实的。另外,我们应该避免在一个特征文件中出现大量的场景。

带背景 的特征文件

题为 “Payment Process”的带背景的特征文件如下-

Feature  Payment Process
   Background:
      Given launch application
      Then Input credentials
   Scenario  Credit card transaction
      Given user is on credit card payment screen
      Then user should be able to complete credit card payment
   Scenario  Debit card transaction
      Given user is on debit card payment screen
      Then user should be able to complete debit card payment
Python

相应的步骤实现文件

文件如下—

from behave import *
@given('launch application')
def launch_application(context):
   print('launch application')
@then('Input credentials')
def input_credentials(context):
   print('Input credentials')
@given('user is on credit card payment screen')
def credit_card_pay(context):
   print('User is on credit card payment screen')
@then('user should be able to complete credit card payment')
def credit_card_pay_comp(context):
   print('user should be able to complete credit card pay')
@given('user is on debit card payment screen')
def debit_card_pay(context):
   print('User is on debit card payment screen')
@then('user should be able to complete debit card payment')
def debit_card_pay_comp(context):
   print('user should be able to complete debit card payment')
Python

输出

运行特征文件后得到的输出结果如下,这里使用的命令是 **behave –no-capture -f plain **

Behave - 背景

继续输出如下 –

Behave - 背景

输出显示了在每个场景前运行两次背景步骤(给定的启动应用程序和然后输入凭证)。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册