Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 977
user3350712 Avatar asked Feb 07 '15 20:02

user3350712


People also ask

How do you use cucumber Gradle?

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.

How do you run a cucumber test in parallel using Gradle?

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.


2 Answers

I opted for not using com.github.samueltbrown.cucumberplugin, 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'
    )
}
like image 82
Miguel Reyes Avatar answered Nov 07 '22 06:11

Miguel Reyes


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
like image 20
Carlo Bellettini Avatar answered Nov 07 '22 07:11

Carlo Bellettini