Set up IntelliJ to run scalatest's FunSpec

I finally set up IntelliJ to be able to run a FunSpec test. For so long, I didn't know why it didn't work until I embarked on a short journey to find out.

My FunSpec test is often complex with set up code and layers of abstractions. For example, I'd use loggedIn instead of it in order to set a user cookie before running a browser.

That was a mistake because it turns out the Scala plugin of IntelliJ specifically looks for the word it. Here's the particular code that I identify: https://github.com/JetBrains/intellij-scala/blob/576a3b5ef47854624c681cad8c5626165d650aee/scala/test-integration/scalatest-finders/src/main/java/org/scalatest/finders/FunSpecFinder.java#L56

After reading the code, I realize that it's fine to overload it, so I overload it to it(testname: String, user: =>User) , and it still works with IntelliJ.

One more thing: for Scala / SBT, you should configure the scalatest template for "Run/Debug Configurations" here:

Go to "Edit configuration templates..."

Go to ScalaTest. Then do the followings:

  1. Select Test kind: to be Test name
    1. This is what IntelliJ will create automatically once it sees it using the Test name template.
  2. Check Use sbt
  3. Under Before launch, remove everything including Build
    1. You don't need to build since you are using SBT. Build is unnecessary and might take 5-30s depending on your setting.

Use UI with sbt doesn't seem to work. I encounter this exact issue: https://youtrack.jetbrains.com/issue/SCL-16240/When-running-ScalaTests-with-Use-UI-with-sbt-Test-framework-quits-unexpectedly which was reported 5 years ago. But it's not really a big deal. It would be nicer to see test results formatted nicely on IntelliJ though.

Now you can run a test within IntelliJ. It's so much better than typing the testOnly command by yourself.

I've been using IntelliJ for years and never really came around to set it up. I just assumed it wouldn't work, and typing the command myself isn't really a huge deal; it only takes a couple seconds with maybe several keystrokes and clicks.

But running it in IntelliJ is a single click!

The morale of the story is that we should spend some time setting up our IDE properly.

Subscribe to tanin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe