将标签传递给 Gradle 黄瓜任务

Passing in tags to Gradle cucumber task

我知道我们可以通过在 build.gradle 文件中指定以下内容将黄瓜标签传递给 Gradle:

test {
    systemProperty "cucumber.options", System.getProperty("cucumber.options")
}

所以当你 运行 gradlew test -Dcucumber.options="--tags @CDC" 它只会 运行s 带有 CDC 标签的场景。

但我的问题是我是否可以将黄瓜选项传递给黄瓜任务。我需要这样做的原因是因为我的黄瓜任务在 运行 任务之后创建了一个 JSON 文件,我正在使用 gradle-cucumber-reporting 插件(https://github.com/SpacialCircumstances/gradle-cucumber-reporting) 根据 JSON 文件生成报告。

黄瓜任务

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = [
            '--plugin', 'html:cucumber-report',
            '--plugin', 'json:cucumber.json', //generates a .json report file
            '--plugin', 'pretty',
            '--plugin', 'usage:cucumber-usage.json',
            '--glue', 'hellocucumber', 'src/test/resources']
        }
    }
}

运行 Gradle 中的 test 任务不会更新 JSON 文件。

这是 gradle-cucumber-reporting 的片段:

cucumberReports {
  outputDir = file('cucumber-pretty-report')
  buildId = '0'
  reports = files('cucumber.json')
}

您可以将类似这样的内容添加到您的黄瓜任务中:

def tags = getProperty("tags")
args = [
'--plugin', 'html:cucumber-report',
'--plugin', 'json:cucumber.json', //generates a .json report file
'--plugin', 'pretty',
'--plugin', 'usage:cucumber-usage.json',
'--glue', 'hellocucumber', 'src/test/resources',
'--tags', tags]

然后你可以这样运行你的任务:

gradle cucumber -P tags=smoke