How do I run a scala ScalaTest in IntelliJ idea?
I'm trying to run a scala flatspec test within Intellij IDEA (latest community build, with latest Scala plugin), but I keep getting "Empty test suite" errors.
I tried using the normal "run" menu on right click, but it does not work. I also tried creating a new ScalaTest configuration, but still the runner is not开发者_高级运维 picking up the tests.
I was able to use JScalaTest with unit, but I'd really prefer to use flatspec syntax.
UPDATE: annotating the class with @RunWith(classOf[JUnitRunner])
does not help either
Thanks!
class SampleTestSpec extends FlatSpec with ShouldMatchers {
"test" should "fail" in {
"this" should equal ("that")
}
}
UPDATE: Switching from ScalaTest to Spec, solved the problem. I still prefer ScalaTest with FlatSpec, but this is good enough. Code that works:
import org.specs._
object SampleTestSpec extends Specification {
"'hello world' has 11 characters" in {
"hello world".size must be equalTo(113)
}
"'hello world' matches 'h.* w.*'" in {
"hello world" must be matching("h.* w.*")
}
}
-teo
If IntelliJ does not pick up the tests automatically, you can do the following:
In the Run Configuration, create a ScalaTest run configuration and set up its "Test kind" to "All in package" and its "Test Package" to the package that contains your tests.
This worked for me: Edit configurations -> Test kind: All in package
I've been running it like this (however I do find it a little verbose and it can probably be cut down):
@RunWith(classOf[JUnitSuiteRunner])
class AuthorisationParserRunAsTest extends JUnit4(AuthorisationParserSpec)
object AuthorisationParserSpec extends Specification {
...
}
ScalaTest has an Overview over all possibilities; I just annotated all test classes with
@RunWith(classOf[JUnitRunner])
AFAIK, the JUnitRunner
is exactly for this.
Just a little too late, but I've met the same problem ("Empty test suite") After hanging around IDEA bug tracker, I've found a solution for my case:
My scala
version was 2.9.0-1
and scala-test
has version 2.8.*
.
It started to work after upgrading scala-test
to the newest version (same as a scala).
I've been able to run FunSuite tests in IntelliJ without any issues. However, what I've found when working with FlatSpec tests, is that you need to right click on or have your cursor located on the actual test class definition if using Ctrl+Shift+F10 (i.e. run all tests) for it to correctly execute all defined tests and avoid the frustrating "Empty test suite" errors.
In case it helps anyone -- I hit this fresh to Scala and it was because of the way I'd structured my test class. I had
class SomeTests extends FlatSpec {
def aTest = {
"This thing when that thing happens" should
"return some particular response" in {
// ... test body
}
}
}
The problem was that I wrapped my test in a method, not realising that in this context the tests need to be bare:
class SomeTests extends FlatSpec {
"This thing when that thing happens" should
"return some particular response" in {
// ... test body
}
}
I ran into "Empty test suite." issue while running individual tests in FlatSpec as well. I narrowed down the root cause to a whitespace at the beginning of the in the behavior string.
Ex. In the following code, the first test will run but the second will not (If individually run). This is due to the presence of a whitespace at the beginning of the behavior string " not run". If you take it off and run it again, it should work.
import org.scalatest.FlatSpec
class FlatSpecWhiteSpaceTest extends FlatSpec {
"TestWithNoEmptySpace" should "run" in {
runTest
}
"TestWithEmptySpace" should " not run" in {
runTest
}
def runTest(): Unit = {
val expectedValue = true
val actualValue = true
assert(expectedValue === actualValue)
}
}
- IDE: Intellij IDeA 2017.1C
- Scala Version: 2.11.8
- ScalaTest Version: 3.0.1
You have to make it Test
at the end of the file and click Run on Sbt
In the test config.
精彩评论