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
or
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.
To run Cucumber with Gradle, make sure that: Gradle is installed. The environment variable GRADLE_HOME is correctly configured. The IDE is configured with the latest Gradle installation.
Cucumber can be executed in parallel using JUnit and Maven test execution plugins. In JUnit the feature files are run in parallel rather than scenarios, which means all the scenarios in a feature file will be executed by the same thread. You can use either Maven Surefire or Failsafe plugin to execute the runners.
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. I.e, 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'
)
}
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With