简体   繁体   中英

How to run the cucumber test using gradle

I'm having trouble running Cucumber tests in Gradle. I am using cucumber-jvm.

Class TestNGCucumberRunner extends the AbstractTestNGCucumberTests and testng annotations with @beforesuite , @aftersuite ..

I usually run the TestNGCucumberRunner.java in IntelliJ by right-click and it runs successfully. Now I want to

  1. Invoke the TestNGCucumberRunner.java in gradle

or

  1. Invoke all the features in gradle

I tried to execute the TestNGCucumberRunner.java as a javaexec but that fails.

I tried to execute all the feature files in the package. I have used apply plugin: 'com.github.samueltbrown.cucumber' also.

UPDATE:

My new setup uses a different plugin that supports parallel execution of scenarios, better reporting, and is still actively maintained:

build.gradle

plugins {
  ...
  id "com.commercehub.cucumber-jvm" version "0.11"
}

addCucumberSuite 'cucumberTest'

dependencies {
  ...
  cucumberTestCompile 'info.cukes:cucumber-java:1.2.5'  // or -java8 if you prefer lambda notation

}

directory structure :

└── src
    ├── cucumberTest
    │   ├── java
    │   │   └── package1 
    │           └──       <- Glue
    │   └── resources    
    │       └── package2 
    │           └──       <- Features
    ├── main
    │   └── java
    └── test
        └── java

package1 and package2 names (together with many other options ) can be specified in the build.gradle file


OLD

My previous setup for using cucumber for java with gradle.

build.gradle

plugins {
    id "java"
    id "com.github.samueltbrown.cucumber" version "0.9"
  }   

dependencies {
    cucumberCompile 'info.cukes:cucumber-java:1.2.4'
}

cucumber {
    formats = ['pretty','junit:build/cucumber.xml']
}

Directory layout

└── src
    ├── cucumber
    │   ├── java         <- Glue
    │   └── resources    <- Features
    └── main
    │    └── java
    └── test
        └── java

Command

gradle cucumber

I opted for not using com.github.samueltbrown.cucumber plugin, it was last updated in Aug 2015. Instead I created a "integrationTest" (cucumber) sourceSet that I can build independently. Ie, a simple gradle build will build test and integrationTest source sets. If I only want to run integration tests I can run gradle integrationTest -x test , or I can run only test with gradle test -x integrationTest .

The steps I followed are here: http://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-integration-testing/

But here is the summary:

build.gradle

sourceSets {
    integrationTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir file('src/integrationTest/java')
        }
        resources.srcDir file('src/integrationTest/resources')
    }
}

//Ensure that the integrationTestCompile/integrationTestRuntime configuration contains the dependencies that are required to compile/run our unit tests.
configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime
}

task integrationTest(type: Test) {
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath

    // Gradle skips tasks whose input and output are up to date.
    // To ensure that your integration tests are run every time,
    // tell Gradle that the outputs of the integrationTest task should always be considered out of date.
    outputs.upToDateWhen { false }
}

// Ensure that our integration tests are run before the check task and that the check task fails the build if there are failing integration tests.
// Ensure that our unit tests are run before our integration tests. This guarantees that our unit tests are run even if our integration tests fails.
check.dependsOn integrationTest
integrationTest.mustRunAfter test

// Ensure that the HTML reports of unit and integration tests are created to different report
// build/reports/integrationTest directory contains the HTML report that contains the test results of our integration tests.
tasks.withType(Test) {
    reports.html.destination = file("${reporting.baseDir}/${name}")
}

def cucumberVersion = "1.2.4"

dependencies {
    integrationTestCompile(
            'info.cukes:cucumber-core:' + cucumberVersion,
            'info.cukes:cucumber-java:' + cucumberVersion,
            'info.cukes:cucumber-java:' + cucumberVersion,
            'info.cukes:cucumber-junit:' + cucumberVersion,
            'info.cukes:cucumber-spring:' + cucumberVersion,
            'org.springframework:spring-beans:4.2.5.RELEASE'
    )
    integrationTestRuntime(
            'org.springframework:spring-context:4.2.5.RELEASE',
            'org.springframework:spring-test:4.2.5.RELEASE',
            'org.springframework:spring-tx:4.2.5.RELEASE'
    )
}

you can read official document.

https://docs.cucumber.io/tools/java/#gradle

The cucumber docs do not use the gradle Test task type, so certain integrations are not there (notably Jacoco). Im still hunting for a way to get a Test task to execute cucumbers main when it runs tests.

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