22
votes

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

10
Would have been nice if you mentioned the actual version numbers. 'Latest' is a moving target.akauppi
IMPORTANT: Depending on circumstances, IntelliJ does not pick up tests cases written with ScalaTest, like FunSpec for example. Make sure you configure the "Run Configuration" -> "ScalaTest" so that the combo "Test kind" says "All in package". I mean: configure the defaults this way, otherwise IntelliJ may or may not create a new configuration the way you would expect.Richard Gomes
IMPORTANT #2 : If you are using SBT, use the sbt-idea plugin in order to generate configuration files for IntelliJ which are needed in when you define "Run Configurations".Richard Gomes
Just right click and run the test isn’t it working?Raman Mishra

10 Answers

18
votes

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.

7
votes

This worked for me: Edit configurations -> Test kind: All in package

4
votes

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 {
  ...
}
2
votes

ScalaTest has an Overview over all possibilities; I just annotated all test classes with

@RunWith(classOf[JUnitRunner])
0
votes

AFAIK, the JUnitRunner is exactly for this.

0
votes

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).

0
votes

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.

0
votes

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
    }

}
0
votes

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
0
votes

You have to make it Test at the end of the file and click Run on Sbt In the test config.