@Author : Terry.Lee
@Time : 2019-10-24 12:16
1. python3安装behave库
pip3 install behave
2. 执行此命令查看中英关键字对照:
behave --lang-help zh-CN
Translations for Chinese simplified / 简体中文
And: 而且<
Background: 背景
But: 但是<
Examples: 例子
Feature: 功能
Given: 假如<
Scenario: 场景
Scenario Outline: 场景大纲
Then: 那么<
When: 当<
3. step装饰器的关键字
__all__ = [
"given", "when", "then", "step", # 首选的写法
"Given", "When", "Then", "Step" # 同样支持这种写法
]
[Given]
在用户或外部系统对被测系统做交互之前,使被测系统处于一个可知的状态。在givens中要避免涉及用户交互的操作。
在use case中,givens可以被理解为前置条件precondition。也可以在Given中使用table来设置一些数据
[When]
描述用户或外部系统施展的关键行为。这个交互行为会引起系统状态的变化
[Then]
观察结果。这个观察的结果应该与feature描述的用户价值business value/benefit相关联。这个结果也应该是一个系统产生的
具体的结果而不是对用户不可见,埋藏得很深的结果。对用户不可见的结果也许会没有business value。比如我们应该多验证用户看得见的结果而不是去数据库里面找结果
注: [and]和[but]会被当做when/given/then的一部分来执行。比如一个and属于given step下,那么and这个step就会变为given的一部分
4. environment.py文件里支持的一些方法:
before_step(context, step), after_step(context, step)
# 夹心饼干,步骤"step"相当于中间的奶油,顺序: before_step > step > after_step
before_scenario(context, scenario), after_scenario(context, scenario)
# 汉堡包,场景"scenario"相当于中间的食材,顺序: before_scenario > scenario > after_scenario
before_feature(context, feature), after_feature(context, feature)
# 大夹心饼干,在每个feature文件包含很多的步骤,就像有很多层奶油,顺序: before_feature > feature > after_feature
before_tag(context, tag), after_tag(context, tag)
# 小蜜蜂,有tag装饰(花)的每个场景前后执行,顺序: before_tag > 被tag装饰的场景 > after_tag
before_all(context), after_all(context)
# 安检员,在你旅程开始和结束时运行,顺序: before_all > 整个测试过程 > after_all
5. 框架的运行顺序
before_all
for feature in all_features:
before_feature
for outline in feature.scenarios:
for scenario in outline.scenarios:
before_scenario
for step in scenario.steps:
before_step
step.run()
after_step
after_scenario
after_feature
after_all