8

I am trying to get a project going using the new Cucumber-jvm system and Gradle as my build system.

I have used the example Java code in the GitHub cucumber-jvm project(https://github.com/cucumber/cucumber-jvm).

My project is set up in IntelliJ and the IDE is able to run the test.

However, Gradle does not find any tests to run. I know this because I broke the test and Gradle said nothing. It also said nothing when it was working.

The class it is trying to run looks like this:

import cucumber.junit.Cucumber;
import cucumber.junit.Feature;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Feature(value = "CarMaintenance.feature")
public class FuelCarTest {
}

I'm new to both cucumber and Gradle!!

1
  • 1
    You need to provide more information, like the full Gradle build script, your source directory layout, the exact Gradle command you executed, etc. Commented Feb 15, 2012 at 16:47

3 Answers 3

6

I remember having trouble with Gradle and Cucumber with the junit runner. I eventually gave up and created a gradle task using the command line runner.

task executeFeatures(type: JavaExec, dependsOn: testClasses) {
    main = "cucumber.cli.Main"
    classpath += files(sourceSets.test.runtimeClasspath, file(webAppDir.path + '/WEB-INF/classes'))
    args += [ '-f', 'html:build/reports/cucumber', '-g', 'uk.co.filmtrader', 'src/test/resources/features']
}

-f Folder for html report output

-g Package name for glue/step code

src/test/resources/features Where the feature files are

With the following dependencies

testCompile 'org.mockito:mockito-all:1.9.5',
            'junit:junit:4.11',
            'org.hamcrest:hamcrest-library:1.3',
            'info.cukes:cucumber-java:1.0.14',
            'info.cukes:cucumber-junit:1.0.14',
            'info.cukes:cucumber-spring:1.0.14'

Update for version 4.2.5

There had been some minor changes over time:

  • the package name of the cli changed to cucumber.api.cli.Main
  • The flag -f seems no longer to be working and causes an error

So I ended up with the following task definition in my build.gradle:

task executeFeatures(type: JavaExec, dependsOn: testClasses) {
    main = "cucumber.api.cli.Main"
    classpath += files(sourceSets.test.runtimeClasspath)
    args += [ '-g', 'uk.co.filmtrader', 'src/test/resources/features'] 
}
Sign up to request clarification or add additional context in comments.

Comments

2

other way can be to create a task and include runner class for test

build.gradle-
task RunCukesTest(type: Test) << {
include "RunCukesTest.class"
}

testCompile 'io.cucumber:cucumber-java:4.2.0'
testCompile 'io.cucumber:cucumber-junit:4.2.0'


your class - 
@RunWith(Cucumber.class)
@CucumberOptions(dryRun = false, strict = true, features = "src/test/resources", glue 
= "com.gradle.featuretests",monochrome = true)
public class RunCukesTest {
}

simply hit the command :- gradle RunCukesTest

Comments

1

Considering:

  • Your .feature files are in src/test/resources/cucumber/features and
  • your glue classes are in com.example.myapp.glue

Then, following what is explained in the docs, you can do in build.gradle:

dependencies {
    // ...
    testImplementation("io.cucumber:cucumber-java:6.2.2")
    testImplementation("io.cucumber:cucumber-junit:6.2.2")
    testImplementation("io.cucumber:cucumber-junit-platform-engine:6.2.2")
}

configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
}

// this enables the task `gradle cucumber`
task cucumber() {
    dependsOn assemble, compileTestKotlin
    doLast {
        javaexec {
            main = "io.cucumber.core.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--strict', '--plugin', 'pretty', '--plugin', 'junit:build/test-results/cucumber.xml', '--glue', 'com.example.myapp.glue', 'src/test/resources/cucumber/features']
        }
    }
}

// (OPTIONAL) this makes `gradle test` also include cucumber tests
tasks.test {
    finalizedBy cucumber
}

Now gradle cucumber will run the cucumber tests.

If you added the last part, gradle test will also run cucumber tests.

The args part supports what goes in the @CucumberOptions annotation of the runner. More details: https://cucumber.io/docs/cucumber/api/#list-configuration-options

5 Comments

I put exactly your code, but when sync it tells me that the compileTestKotlin is an unknown property for task ':app:cucumber'. What have you done to make it work?
@GabrielGarcia Is your gradle a multi-project one? If so, perhaps the build.gradle in which you declare the task cucumber() matters
Thank you for the fast response. It is a single app android project build with Kotlin. I included all this in the app module build.gradle.
Ah, ok. Can you instead of dependsOn assemble, compileTestKotlin try dependsOn assemble, testClasses?
Nothing. Same error. i am switching into cucumber-android and hope it works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.