Jerrico Gamis <[email protected]> :toc: :icons: :website: https://github.com/jecklgamis/cucumber-jvm-scala-example
This is an example Cucumber-JVM project.
-
Uses Scala step definitions
-
Packages tests into executable jar file
If you’re new to Cucumber testing, start with https://cucumber.io/docs
. Otherwise, run the example
test in IDE or command line (see Running Cucumber Tests).
-
Feature files - contains the BDD feature definitions (Gherkin, e.g.
Example.feature
) -
Step definitions - Implementation of the step definitions (Scala, e.g.
ExampleSteps.scala
) -
CucumberTestMain - this is the program entry point when tests are packaged as executable jar file (e.g. target/cucumber-jvm-scala-example.jar)
-
Test runners - run specific feature set ( Scala with Junit annotations, e.g.
ExampleTestRunner.scala
). This is only used when developing tests.
Intellij :
-
Install Scala plugin
-
Install Cucumber for Scala plugin (allows easy navigation of glue steps)
-
Create or update feature file in
src/main/resources/features
-
Create or update test runner in
src/test/scala/runner
with the appropriate tags -
Implement or update steps in
src/main/scala/steps
Feature: Example feature @ExampleFeature Scenario: Example scenario Given this pre condition And this pre condition When I do this And I do this Then I can verify that And I can also verify that
See the example below and change the appropriate values of the CucumberOptions annotation.
@RunWith(classOf[Cucumber]) @CucumberOptions( features = Array("classpath:features/Example.feature"), tags = Array("~@Wip"), glue = Array("classpath:steps"), plugin = Array("pretty", "html:target/cucumber/html")) class ExampleFeatureRunner
-
This runs all scenarios in
Example.feature
not tagged with@Wip
-
The step definitions are loaded from class path
steps
-
This generates reports in
target/cucumber/html
folder relative to some execution path
After you created the feature files, run the test runner, it will give you hints on the missing steps you need to implement
Example (ExampleSteps.scala
)
Given("""^this pre condition$""") { () => //// Write code here that turns the phrase above into concrete actions } When("""^I do this$""") { () => //// Write code here that turns the phrase above into concrete actions } Then("""^I can verify that$""") { () => //// Write code here that turns the phrase above into concrete actions } Then("""^I can also verify that$""") { () => //// Write code here that turns the phrase above into concrete actions } }
Using maven test:
mvn test
Using executable jar file:
mvn clean package java -jar target/cucumber-jvm-scala-example.jar --plugin pretty --plugin html:cucumber/html --json json:cucumber/json/cucumber.json --glue steps classpath:features --tags ~@Wip
or using cucumber.options
environment variable:
mvn clean package java -Dcucumber.options="--plugin pretty --plugin html:cucumber/html --plugin json:cucumber/json/cucumber.json --tags ~@Wip --glue steps classpath:features" -jar target/cucumber-jvm-scala-example.jar
Using maven exec:plugin:
mvn exec:java -Dcucumber.options="--plugin pretty --plugin html:cucumber/html --plugin json:cucumber/json/cucumber.json --tags ~@Wip --tags @ExampleFeature"
The above command line examples generate reports in cucumber/html
and in cucumber/json
directories
In the Run/Debug Configuration
, add the steps directories (in this example, steps
) in the Glue
text field.
-
A number of options here, instance variables, thread local map, or containers