How do I run a scala ScalaTest in IntelliJ idea?
Asked Answered
M

10

23

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

Mendelian answered 22/12, 2010 at 11:43 Comment(4)
Would have been nice if you mentioned the actual version numbers. 'Latest' is a moving target.Hazy
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.Codling
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".Codling
Just right click and run the test isn’t it working?Incantatory
B
4

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 {
  ...
}
Britteny answered 22/12, 2010 at 13:6 Comment(1)
So, I switched from ScalaTest to Specification and now it works. I can confirm you don't need the boilerplate class nor the @RunWith: import org.specs._ object AuthorisationParserSpec extends Specification { will doMendelian
P
18

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.

Pedestrianize answered 25/7, 2013 at 8:17 Comment(0)
C
8

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

Crystalcrystalline answered 21/7, 2015 at 23:6 Comment(0)
B
4

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 {
  ...
}
Britteny answered 22/12, 2010 at 13:6 Comment(1)
So, I switched from ScalaTest to Specification and now it works. I can confirm you don't need the boilerplate class nor the @RunWith: import org.specs._ object AuthorisationParserSpec extends Specification { will doMendelian
P
2

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

@RunWith(classOf[JUnitRunner])
Prostyle answered 15/2, 2013 at 14:49 Comment(0)
B
0

AFAIK, the JUnitRunner is exactly for this.

Bermudez answered 22/12, 2010 at 12:24 Comment(0)
F
0

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

Feodore answered 24/7, 2011 at 0:15 Comment(0)
F
0

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.

Facia answered 2/2, 2015 at 23:3 Comment(0)
M
0

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
    }

}
Merino answered 14/7, 2016 at 11:26 Comment(0)
G
0

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
Gnathous answered 29/6, 2018 at 20:9 Comment(0)
S
0

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

Salience answered 6/2, 2020 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.