|
| 1 | +Cucumber JUnit |
| 2 | +============== |
| 3 | + |
| 4 | +Use JUnit to execute cucumber scenarios. |
| 5 | + |
| 6 | +Add the `cucumber-junit` dependency to your pom. |
| 7 | + |
| 8 | +```xml |
| 9 | +<dependencies> |
| 10 | + [...] |
| 11 | + <dependency> |
| 12 | + <groupId>io.cucumber</groupId> |
| 13 | + <artifactId>cucumber-junit</artifactId> |
| 14 | + <version>${cucumber.version}</version> |
| 15 | + <scope>test</scope> |
| 16 | + </dependency> |
| 17 | + [...] |
| 18 | +</dependencies> |
| 19 | +``` |
| 20 | + |
| 21 | +Create an empty class that uses the Cucumber JUnit runner. |
| 22 | + |
| 23 | +```java |
| 24 | +package com.example; |
| 25 | + |
| 26 | +import cucumber.api.CucumberOptions; |
| 27 | +import cucumber.api.junit.Cucumber; |
| 28 | +import org.junit.runner.RunWith; |
| 29 | + |
| 30 | +@RunWith(Cucumber.class) |
| 31 | +@CucumberOptions(plugin = "json:target/cucumber-report.json") |
| 32 | +public class RunCukesTest { |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +This will execute all scenarios in same package as the runner, by default glue code is also assumed to be in the same |
| 37 | +package. The `@CucumberOptions` can be used to provide |
| 38 | +[additional configuration](https://cucumber.io/docs/reference/jvm#list-configuration-options) to the runner. |
| 39 | + |
| 40 | + |
| 41 | +## Using JUnit Rules ## |
| 42 | + |
| 43 | +Cucumber supports JUnits `@ClassRule`, `@BeforeClass` and `@AfterClass` annotations. These will executed before and |
| 44 | +after all features. Using these is not recommended as it limits the portability between different runners; they may not |
| 45 | +execute correctly when using the commandline, [IntelliJ IDEA](https://www.jetbrains.com/help/idea/cucumber.html) or |
| 46 | +[Cucumber-Eclipse](https://github.com/cucumber/cucumber-eclipse). Instead it is recommended to use Cucumbers `Before` |
| 47 | +and `After` hooks. |
| 48 | + |
| 49 | +## Using other JUnit features ## |
| 50 | + |
| 51 | +The Cucumber runner acts like a suite of a JUnit tests. As such other JUnit features such as Categories, Custom JUnit |
| 52 | +Listeners and Reporters can all be expected to work. |
| 53 | + |
| 54 | +For more information on JUnit, see the [JUnit web site](http://www.junit.org). |
0 commit comments