Search code examples
javagradlecucumbercucumber-jvm

Passing in tags to Gradle cucumber task


I know that we can pass cucumber tags to Gradle by specifying the following in build.gradle file:

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

So when you run gradlew test -Dcucumber.options="--tags @CDC" it will only runs scenarios with CDC tag.

But my question is whether I can pass the cucumber options to the cucumber task. The reason I need to do this is because my cucumber task creates a JSON file after running the task, and I'm using gradle-cucumber-reporting plugin (https://github.com/SpacialCircumstances/gradle-cucumber-reporting) to generate a report based on the JSON file.

cucumber task

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']
        }
    }
}

Running the test task in Gradle doesn't update the JSON file.

Here's the snippet for gradle-cucumber-reporting:

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

Solution

  • You can add something like this to your cucumber task:

    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]
    

    Then you can run your task this way:

    gradle cucumber -P tags=smoke