Skip to content

Add example of running Cucumber-JVM tests using Gradle #446

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jan 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions examples/java-gradle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Running Cucumber-JVM with step definitions in Java using Gradle `javaexec` task

## Credits

This work is based on [dkowis/cucumber-jvm-groovy-example](https://github.com/dkowis/cucumber-jvm-groovy-example)

## Motivation

There exists [a number of issues](http://gradle.1045684.n5.nabble.com/Gradle-and-cucumber-jvm-tt5710562.html) which prevent seamless integration of Cucumber-JVM and Gradle.

## Solution

One possible solution is to use Cucumber's `Main` class to run your tests. You can do this by using `javaexec` task in Gradle.

## Running

In order to run your Cucumber tests execute:

```sh
gradle cucumber
```

## Caveats

Groovy example by [David Kowis](https://github.com/dkowis) runs perfectly, but it uses Groovy step definitions.

If you're writing your step definitions in Java then Gradle script needs to be changed slightly.

Here are some caveats:

* `cucumber` task has to depend on `compileTestJava` task in order to compile test sources

```groovy
task cucumber() {
dependsOn assemble, compileTestJava
...
}
```


* `javaexec` classpath should include `main` and `test` output directories.
Otherwise Cucumber-JVM will not find your production classes/resources and step definitions respectively.

```groovy
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
```

* Cucumber's `--glue` should be set to your package name (e.g. `gradle.cucumber`) and **NOT** to `src/test/java`

```groovy
args = ['-f', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
```



29 changes: 29 additions & 0 deletions examples/java-gradle/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apply plugin: 'java'

configurations {
cucumberRuntime {
extendsFrom testRuntime
}
}

task cucumber() {
dependsOn assemble, compileTestJava
doLast {
javaexec {
main = "cucumber.api.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = ['-f', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
}
}
}

dependencies {
testCompile 'info.cukes:cucumber-java:1.1.1'
testCompile 'info.cukes:cucumber-junit:1.1.1'

testCompile 'junit:junit:4.11'
}

repositories {
mavenCentral()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gradle.cucumber;

public class Production {

public void doWork() {
throw new RuntimeException("production failed here");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gradle.cucumber;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;

public class BasicStepdefs {

@Given("^I use Cucumber Main class to run tests$")
public void I_use_Cucumber_Main_class_to_run_tests() throws Throwable {
//do nothing
}

@When("^I run failing test$")
public void I_run_failing_test() throws Throwable {
new Production().doWork();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gradle.cucumber;

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Cucumber.Options(format = {"pretty", "html:build/cucumber-html-report", "json-pretty:build/cucumber-report.json"})
public class RunCukesTest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature: Gradle-Cucumber integration

Scenario: Gradle reports 'BUILD FAILED' when Cucumber test fails
Given I use Cucumber Main class to run tests
When I run failing test
Then Gradle should report "BUILD FAILED"