参数化功能文件的一种方法是在编译时从模板生成它。然后在运行时你的黄瓜运行器执行生成的功能文件。
如果您使用 gradle,这很容易做到。这是一个例子:
在 build.gradle 中,添加 groovy 代码,如下所示:
import groovy.text.GStringTemplateEngine
task generateFeatureFiles {
doFirst {
File featuresDir = new File(sourceSets.main.output.resourcesDir, "features")
File templateFile = new File(featuresDir, "myFeature.template")
def(String bestDay, String currentDay) = ["Friday", "Sunday"]
File featureFile = new File(featuresDir, "${bestDay}-${currentDay}.feature")
Map bindings = [bestDay: bestDay, currentDay: currentDay]
String featureText = new GStringTemplateEngine().createTemplate(templateFile).make(bindings)
featureFile.text = featureText
}
}
processResources.finalizedBy(generateFeatureFiles)
myFeature.template 位于 src/main/resources/features 目录中,可能如下所示:
Feature: Is it $bestDay yet?
Everybody wants to know when it's $bestDay
Scenario: $currentDay isn't $bestDay
Given today is $currentDay
When I ask whether it's $bestDay yet
Then I should be told "Nope"
运行构建任务将在 build/src/main/resources 中创建一个 Friday-Sunday.feature 文件,并填写 bestDay 和 currentDay 参数.
generateFeatureFiles 自定义任务在 processResources 任务之后立即运行。生成的特征文件然后可以由黄瓜运行器执行。
您可以从功能模板文件生成任意数量的功能文件。例如,代码可以从资源目录中的配置文件中读取参数。