-->

How do you run cucumber with Scala 2.11 and sbt 0.

2020-08-26 11:19发布

问题:

Does anyone have an example of a cucumber project with sbt 0.13 and Scala 2.11?

  1. Do I need both the cucumber-scala_2.11 and the sbt-cucumber-plugin" % "0.8.0" (is that plugin up to date)?
  2. Where does the plugin go now?
  3. Where do the .feature files go?
  4. Where do the cucumber tests go?
  5. How do I run the tests from sbt?
  6. (optional) How can I run the tests from IntellJ (15)?

回答1:

Ok, I figured out a solution with the following caveat: I'm not using sbt.

Idea

We will write cucumber features and steps first. Then we will write a "Runner" class, that will be run by JUnit runner (which will be oblivious to the presence of cucumber)

Procedure

Step 1. Depend only on one thing for writing cucumber features and steps!

libraryDependencies += "info.cukes" % "cucumber-scala_2.11" % "1.2.4"

Step 2: Now we depend on junit (for writing the runner of our tests) and cucumber-junit connection library that will allow us to tell JUni to run our cucumber tests:

libraryDependencies += "info.cukes" % "cucumber-junit" % "1.2.4"
libraryDependencies += "junit" % "junit" % "4.12"

Step 3: Write feature and steps definitions (they should be placed in the same folder in tests in my experience):

# My.feature
Feature: blah blah
  ....

// MySteps.scala
...

Step 4: Write our test runner:

import cucumber.api.junit.Cucumber
import org.junit.runner.RunWith
@RunWith(classOf[Cucumber])
class RunTests extends {
}

Step 5: (optional) Integration with IntelliJ

a) Enable JUnit in IntelliJ. This will allow us to run our cucumber tests by running our junit runner. Beautiful!

  1. File

  2. Settings...

  3. Plugins

  4. Search for "JUnit" and make sure it's enabled

    Now we can run our cucumber tests by simply right clicking on the .feature file and selecting Run!

b) Enable Cucumber for Scala IntelliJ plugin. (make sure Scala plugin is enabled). This enables IntelliJ to determine how to associate feature files with scala files. Without this your feature files will always be highlighted in yellow with an error undefined step reference:

  1. Files

  2. Settings...

  3. Plugins

  4. Browse repositories...

  5. search for "Cucumber for Scala"

  6. Install Cucumber for Scala plugin

  7. Restart IntelliJ.

    Now your feature files will be highlighted properly!

Useful Resources

  1. Java tutorial for cucumber, that led me to this solution: https://c0deattack.wordpress.com/2012/03/28/cucumber-jvm-with-cucumber-java-cucumber-junit-example/
  2. Cucumber chat room where people are super helpful: https://gitter.im/cucumber/chat
  3. Directory structure (note that in scala we seem to need to place both feature and steps files into the same folder: however the idea of individual runners is interesting): http://www.hascode.com/2014/12/bdd-testing-with-cucumber-java-and-junit/#Directory_Structure


回答2:

To add to @drozzy 's answer, specifically for sbt: add junit-interface as a dependency in your sbt project

"com.novocode" % "junit-interface" % "0.11" % Test

Write the test runner as @drozzy shows at step 4. Make sure you include the connection between JUnit and Cucumber:

@RunWith(classOf[Cucumber])

Once you do that, sbt test will run your JUnit tests, and one of your "JUnit tests" will run your Cucumber tests.