- Behave教程
 - Behave - 主页
 - Behave - 简介
 - Behave - 安装
 - Behave - 命令行
 - Behave - 配置文件
 - Behave - 功能测试设置
 - Behave - 小Cucumber关键词
 - 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 - 可选部分
功能文件中可能有一些步骤具有几乎相似的短语。Behave 具有解析能力,因此一个步骤定义就可以涵盖这些步骤。use_step_parser方法用于此目的,我们必须将解析器类型作为参数传递给该方法。
对于扩展解析匹配器,我们必须传递参数 cfparse。它具有基数字段 (CF) 支持。默认情况下,它会为连接的基数生成缺失的类型转换器(如果给出的基数等于 1 的类型转换器)。
它可以支持以下解析表达式 -
{values:Type+} – 基数=1..N,很多
{值:类型*} – 基数=0..N,many0
{values:Type?} – 基数=0..1,可选
特征文件(几乎类似的步骤)
具有几乎相似步骤的功能文件如下 -
Feature − Payment Process
Scenario − Check Debit transactions
      Given user is on "debit" screen
   Scenario − Check Credit transactions
      Given user is on "credit" screen
方法register_type用于注册用户定义的类型,可以在匹配步骤时解析该类型以进行任何类型转换。
对应步骤实施文件
步骤实施文件如下 -
from behave import *
import parse
#define parse type
use_step_matcher("cfparse")
# for whitespace characters
@parse.with_pattern(r"x\s+")
def parse_string(s):
#type converter for "x" succeeded by single/multiple spaces
   return s.strip()
#register user-defined datatype
register_type(x_=parse_string)
#optional part :x_? cardinality field in parse expression
@given('user is on {:x_?}{payment} screen')
def step_payment(context, x_, payment):
   print("Payment type: ")
   print(payment)
输出
运行功能文件后获得的输出如下所示,使用的命令是beeve --no-capture -f plain。
输出显示借方和贷方。这两个值已在功能文件中通过几乎相似的步骤传递。在步骤实现中,我们使用解析表达式中的基数字段解析了这两个步骤。