Search code examples
gradlecucumber-jvm

Cucumber-JVM with Gradle, how to run tests automatically


I'm working on a Java project that uses Gradle as its build system.

I want to add some bdd tests using Cucumber-JVM. Following this example I was able to configure Gradle's build.gradle to have a task called cucumber, and I was able to execute that task using "gradle cucumber".

But what I am looking for is a way to have Gradle run that task automatically during its test phase (where it runs all the other regular unit tests). I also want the build to be flagged as failed if any of the cucumber tests fail (strict=true).

Is this possible? I don't know a lot about Gradle and Google so far has produced nothing really useful.


Solution

  • Okay so I figured out how to achieve this. Simply add this to build.gradle:

    test << {
            javaexec {
                main = "cucumber.api.cli.Main"
                classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
                args = ['--strict', '--monochrome', '--plugin', 'pretty', '--glue', 'com.mypackage', 'src/test/resources']
            }
    }
    

    All you really need are the feature file and the steps java files that implement the glue code. You do not need the xxxCukesTest.java file with a @RunWith annotation since Gradle ignores it. You may want to keep it anyway because it enables you to run tests from your IDE.

    Works really neat!