rdx / behat-variables
在Behat测试期间跨场景存储变量
1.2
2017-11-16 11:58 UTC
Requires
- behat/behat: ^3.0
Requires (Dev)
- symfony/process: ^2.1
README
在功能测试期间跨场景存储自定义变量。
想法
您正在创建用户和配置文件进行测试,并希望在相同的特性中进一步使用这些创建。您无法这样做,因为:1) Behat参数是字面量,2) FeatureContext只为每个场景存在,而不是整个特性。
使用behat-variables,您可以保存并使用这些变量:用户ID、密码、激活令牌等。
Given a new user "Fred" # Your custom step, with a return value
And I save it into "UID" # A provided step that saves that return value
When I go to "/users/<<UID>>" # Custom step, with dynamic argument
Then I should see "Hello, Fred!" # Custom step, with predictable content
<<UID>>
部分是魔法。适用于任何参数,而不仅仅是URI。
设置
为了在您的Behat功能中使用变量,您必须做两件事
- 添加功能上下文类:
rdx\behatvars\BehatVariablesContext
- 添加扩展类:
rdx\behatvars\BehatVariablesExtension
查看Behat文档了解它们在behat.yml
中的位置,或查看此存储库的behat.yml
default:
suites:
default:
contexts:
- rdx\behatvars\BehatVariablesContext
- FeatureContext
extensions:
rdx\behatvars\BehatVariablesExtension: ~
并确保您的自定义步骤有标量返回值或标量数组
/**
* @Given a value :value
*/
public function aValue($value) {
return $value;
}
/**
* @Given values :value1 and :value2
*/
public function valuesAnd($value1, $value2) {
return [$value1, $value2];
}
示例
查看features/simple.feature
以获取非常简单的示例(仅3个自定义步骤)。这是测试此包使用的测试。
特性
此包提供1个步骤,以几种格式提供
(I|we) save (it|that|those|them) into "VARIABLE_NAME"
因此您可以创建多个自定义步骤组合
Given "4" cars in the same shop # Custom
And we save those into "CAR1,CAR2,CAR3,CAR4" # Provided
Given a user "Fred" in organization "McDonald's" # Custom
And we save those into "USER,ORGANIZATION" # Provided