问题
Does anyone have an example of a cucumber project with sbt 0.13 and Scala 2.11?
- Do I need both the
cucumber-scala_2.11
and thesbt-cucumber-plugin" % "0.8.0"
(is that plugin up to date)? - Where does the plugin go now?
- Where do the
.feature
files go? - Where do the cucumber tests go?
- How do I run the tests from sbt?
- (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!
- File
- Settings...
- Plugins
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
:
- Files
- Settings...
- Plugins
- Browse repositories...
- search for "Cucumber for Scala"
- Install
Cucumber for Scala
plugin Restart IntelliJ.
Now your feature files will be highlighted properly!
Useful Resources
- 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/
- Cucumber chat room where people are super helpful: https://gitter.im/cucumber/chat
- 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.
来源:https://stackoverflow.com/questions/33020504/how-do-you-run-cucumber-with-scala-2-11-and-sbt-0-13