Pregunta

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{

  }



}
¿Fue útil?

Solución

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.

Otros consejos

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

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

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.

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.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top