简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM