Running individual ScalaTest test methods in IntelliJ IDEA
Asked Answered
L

6

24

It is possible to run a single, selected JUnit test method in IntelliJ IDEA 12, however this does not seem to be possible with ScalaTest. You can run the whole test class or nothing, but there seems to be no finer grained control for ScalaTest (yet) in IntelliJ IDEA 12. (I haven't tried IDEA 13 yet.)

So, the question: Is there a way to execute only a single, selected ScalaTest test method in IntelliJ (like it is possible with JUnit test methods.)

Below is a code sample whose test methods I'd like to run individually in IntelliJ. Any idea how to do that?

I've tried JUnitRunner but that did not help.

class NodeDAOTest extends FlatSpec with SessionAware with BeforeAndAfter {

  before{ IM3_SessionFactory.removeData
  println("before is running")}
  println("NodeDAOTest constructor.")
  def numberOfNodes=initAndCloseDB(transaction{NodeDAO.numberOfNodes})


  "Node" can "be added to DB and removed." in {
    val n =new TextNode
     assert(numberOfNodes===0)

    initAndCloseDB { transaction{session save n}}
     assert(numberOfNodes===1)

    initAndCloseDB { transaction{deleteNode(n)}}
     assert(numberOfNodes===0)
  }

  def getTag= initAndCloseDB {transaction{ session.createQuery("from Tag").list().get(0).asInstanceOf[Tag]}}
  def getNode=initAndCloseDB {transaction{ session.createQuery("from Node").list().get(0).asInstanceOf[Node]} }

  it can "be tagged and untagged" in {
    val t=new Tag
    val n=new TextNode

    assert(numberOfNodes==0,"before adding one tag (and Node), there should be 0 Node in the DB")

    initAndCloseDB{ transaction {addTag(t,n) }}

    assert (getNode.getNumberOfTags===1)
    assert (getTag.getNodes.size===1)

    initAndCloseDB(transaction{removeTag(t,n)})

    assert (numberOfNodes==1,"after removing the tag, there should be 1 Node in the DB")
    assert (getNode.getNumberOfTags===0)
    assert (getTag.getNodes.size===0)
  }

  "Tagged Node" can "be removed." in {
    val f=new TagAndNodeFixture
    assert(numberOfNodes==1)
    initAndCloseDB{ transaction {addTag(f.t,f.n) }}
    initAndCloseDB { transaction{deleteNode (f.n)} }
    assert(numberOfNodes==0)
    // the tag will be removed from the node
  }

  "Tag" can "be deleted and that will be removed from in-memory Nodes" in{

  }



}
Lullaby answered 25/1, 2014 at 16:30 Comment(0)
K
17

I use IntelliJ IDEA 13 and I am able to run single test when I use FunSuite - put cursor in a test and press Alt + Shift + F10 and the test is there. It is not possible with FlatSpec tests.

It has been added recently and I remember it wasn't working in version 12 even for FunSuite.

Knap answered 25/1, 2014 at 20:33 Comment(2)
Really? It does give me the option, but when I try, all tests on the spec class run I try it but I only get a all high level groupings (should, when) but no actual tests are run. It says "Empty test suite". My tests look like this: "Setup for a day" should { "not go past the end of the day (1440 minutes)" in { val calendar = emptyCalendar calendar ! new Setup(DateTimeZone.UTC, Map(1 -> Seq(new Segment(2240, 1)))) assert(receiveOne(responseTime).isInstanceOf[Failure]) }Morality
This now works with FlatSpec as well. The command on the Mac is Control-Option-"R" or search for "Run..."Lola
A
7

You can generate a run configuration for a specific flatspec test by putting your cursor into the test, and from the Run menu select Run... (option+Shift+F10 on mac), and near the top will be an entry for the specific test.

You can manually generate the run configuration by selecting your test class as normal, then copying in the test name (The "foo" in "foo" should "bar" in...) into the Test Name field

Advantageous answered 31/1, 2014 at 15:15 Comment(3)
FYI - if your test name spans more than one line, Alt/Option Shift F10 won't work, but manually populating the Test Name field as mentioned above will.Porker
Weird -- and yet you can't right-click on a single test and run it.Intwine
I can't attach an image, but if I run a FlatSpec, then right-click on a test case entry for that spec in the run dialog, the only thing in the right-click menu is "Navigate to testdata". If I do the same thing on a JUnit test, I can run it.Intwine
A
5

I had the same problem. It shows up, if you have space in the first word

"test " should "..."

When i removed space, the test starts to run

Alanson answered 8/4, 2015 at 9:44 Comment(2)
Wow, my biggest respect for figuring out this one! Fixed it!Necklace
This is absurd, I can't believe I wasted an hour and this was the cause. ty!Schoenburg
M
3

A comment on the above: Say this is your test set:

"A calendar" should {
    "not have any availability" in {...}
}

or

"A calendar" when {
    "empty" should {
        "not have any availability" in {... }
    }
}

If you right-click o the second (or third) line (or use the shortcuts in other answers: Alt Shift F10 or option+Shift+F10 on mac) IntelliJ will give you an option to create a launch configuration "Spec.not..." which when run will say "Empty test suite".

The reason is that the "test name" is incorrectly created. Edit your launch configuration to use the full name: "A calendar should not have..." or "A calendar when empty should not have..." and it will work. Seems to be a problem parsing what the name should be from the code.

Morality answered 4/2, 2014 at 0:19 Comment(1)
In addition, if you have a "A calendar" should... followed by a it should ... test, the auto-generated test name for the former will pick the name of the following it should test.Trilemma
T
1

I ran into the same problem with some test suites but not others. I.e. on some right clicking on a test would run the whole suite, while others I could run just one test.

What I finally determined was that a test suite that defined a method in the same scope as the specs would not allow me to run individual tests. Putting those methods inside an object and then importing that object was the easiest refactor to get them out of scope without having to change the test. I.e.

class MySpec extends FlatSpec with Matchers {

  "I" should "be able to run just this test" in {
     multiply(2,3) shouldBe 6
  }

  def multiply(a: Int, b: Int): Int = a * b

}

becomes

class MySpec extends FlatSpec with Matchers {

  import Helpers._
 
  "I" should "be able to run just this test" in {
     multiply(2,3) shouldBe 6
  }

  object Helpers {
    def multiply(a: Int, b: Int): Int = a * b
  }
}

The exception is that any test with behaves like once again breaks the ability to run any single test in the entire suite. And I have not found a syntactic trick to make those work with the IntelliJ runner.

Trilemma answered 3/7, 2020 at 20:58 Comment(0)
M
1

The question does not mention the build tool that was used. I had this problem for a specific build tool: Gradle. What solved it for me was to click on settings (the little wrench icon) then Build,Execution,Deployment, Then Build Tools, Then Gradle. In the panel for Gradle projects I selected Run tests using "Intellij IDEA". After I selected that I had fine grained control over which individual tests I could run.

Pros and cons of running tests using IDEA vs. Gradle are discussed in the Jetbrains post below, which also has a nice screen shot of what you need to select: https://www.jetbrains.com/help/idea/work-with-tests-in-gradle.html#configure_gradle_test_runner

Gradle: IntelliJ IDEA uses Gradle as a default test runner. As an outcome, you get the same test results on the continuous integration (CI) server. Also, tests that are run in the command line will always work in the IDE.

IntelliJ IDEA: select this option to delegate the testing process to IntelliJ IDEA. In this case, IntelliJ IDEA uses the JUnit test runner and tests are run much faster due to the incremental compilation.

Marquis answered 16/2, 2021 at 2:18 Comment(1)
Build, Execution, Deployment > Build Tools > Gradle ... has option "Run tests using" (Gradle by default and IntelliJ Idea). I chose the second option and now I able to run any test separatelyShortstop

© 2022 - 2024 — McMap. All rights reserved.