Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cucumber-jvm tests using Gradle

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!!

like image 772
FinalFive Avatar asked Feb 15 '12 11:02

FinalFive


1 Answers

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'] 
}
like image 133
Mike Rocke Avatar answered Oct 21 '22 12:10

Mike Rocke