- Behave 教程
- Behave - 首页
- Behave - 简介
- Behave - 安装
- Behave - 命令行
- Behave - 配置文件
- Behave - 功能测试设置
- Behave - Gherkin 关键字
- Behave - 功能文件
- Behave - 步骤实现
- Behave - 初级步骤
- Behave - 支持的语言
- Behave - 步骤参数
- Behave - 场景大纲
- Behave - 多行文本
- Behave - 设置表
- Behave - 步骤中的步骤
- Behave - 背景
- Behave - 数据类型
- Behave - 标签
- Behave - 枚举
- Behave - 步骤匹配器
- Behave - 正则表达式
- Behave - 可选部分
- Behave - 多方法
- Behave - 步骤函数
- Behave - 步骤参数
- Behave - 运行脚本
- Behave - 排除测试
- Behave - 重试机制
- Behave - 报告
- Behave - 钩子
- Behave - 调试
- Behave 有用资源
- Behave - 快速指南
- Behave - 有用资源
- Behave - 讨论
Behave - 背景
添加背景是为了将一组步骤组合在一起。它类似于场景。我们可以使用背景为多个场景添加上下文。它在功能的每个场景之前运行,但在执行 before 钩子之后。
背景通常用于执行前提条件,例如登录场景或数据库连接等。
可以添加背景描述以提高人类可读性。它在一个功能文件中只能出现一次,并且必须在场景或场景大纲之前声明。
不应使用背景来创建复杂的状态(除非无法避免)。此部分应简短而准确。此外,我们应该避免在一个功能文件中包含大量场景。
包含背景的功能文件
名为“支付流程”的功能的包含背景的功能文件如下所示:
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
相应的步骤实现文件
文件如下所示:
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')
输出
运行功能文件后获得的输出如下所示,此处使用的命令为behave --no-capture -f plain。
接下来的输出如下:
输出显示背景步骤(Given 启动应用程序 & Then 输入凭据)在每个场景之前运行两次。
广告