Behave 枚举
Enumeration is used to map the multiple distinctive string based words to the values.
我们可能需要具有以下特征的用户定义数据类型:
-
少数单词必须匹配。
-
测试执行之前的预定义值。
对于上述场景,可以使用基于字符串的枚举。
特征文件
考虑一个Feature文件,标题为payment process,如下所述:
Feature: Payment Process Scenario: Response When User asks "Is payment done?" Then response is "No"
在步骤实现文件中,TypeBuilder.make_enum 函数为提供的单词或字符串枚举计算正则表达式模式。方法 register_type 用于注册一个用户定义的类型,该类型可以在匹配步骤时被解析为任何类型转换。
此外,我们将传递参数:包含在“{}”中的用户定义枚举数据类型。
对应步骤实现文件
上述 Feature 的 step 实现文件如下:
from behave import * from behave import register_type from parse_type import TypeBuilder # -- ENUM: Yields True (for "yes"), False (for "no") parse_response = TypeBuilder.make_enum({"yes": True, "no": False}) register_type(Response=parse_response) @when('User asks "{q}"') def step_question(context, q): print("Question is: ") print(q) @then('response is "{a:Response}"') def step_answer(context, a): print("Answer is: ") print(a)
运行特征文件后得到的输出如下所述。在这里,我们使用了命令 Behave --no-capture -f plain .
输出显示 付款完成了吗? and False .输出 False 来自枚举数据类型。