Run, debug and test Scala
IntelliJ IDEA lets you run, debug and test your Scala applications as you would normally do with any other applications in IntelliJ IDEA. IntelliJ IDEA also lets you run your Scala code with coverage and configure code coverage settings.
Run Scala applications
You can run your Scala code through IntelliJ IDEA, use sbt shell, or use Scala worksheet for a quick code evaluation.
Run a Scala application via Intellij IDEA
Create or import a Scala project as you would normally create or import any other project in IntelliJ IDEA.
Open your application in the editor.
Press Shift+F10 to execute the application. Alternatively, in the left gutter of the editor, click the icon and select Run 'name'.
Run a Scala application using the sbt shell
You can run your application using the sbt shell that is a part of any sbt project.
Open your sbt project.
If you want to delegate your builds and imports to sbt, in the sbt projects tool window, click the icon to open the sbt settings.
On the sbt settings page, select the Use sbt shell for build and import (required sbt 0.13.5+) option and click OK. (This option is also available when you create or import an sbt project.)
If you don't want to delegate your builds and imports to sbt, you can still work in the sbt shell () and run sbt commands directly from it including running your application.In the sbt projects tool window, click sbt Tasks node.
In the list that opens, select the run task that will run a main method. The result of execution is displayed in sbt shell tool window.
Run Scala code using Scala worksheet
IntelliJ IDEA lets you create a Scala worksheet to evaluate your Scala code results on the fly.
- Right-click on your project and select New|Scala Worksheet.
We recommend that you create your worksheet in src directory to simplify an inclusion of the project's classes in the classpath. That might be helpful for testing purposes.
- In the New Scala Worksheet window, type a name of your Scala worksheet and click OK.
As a result, a file with .sc extension opens.
Enter your code and press to see the results.
- While working with the .sc file, you can perform the following actions:
You can evaluate a Scala object.
You can fold the output without affecting your code on the left side and expand only that block of the output that matches a specific statement.
- You can click the settings icon on the toolbar of the worksheet editor and configure the current worksheet settings or default worksheet settings for the whole project that will affect other worksheets.
If you want to see the results automatically, select the Interactive Mode checkbox.
Clear the Make project checkbox to improve evaluation performance. In this case, the automatic checking of project's changes is disabled.
You can select the Run type option to see different evaluation modes. For example, select the REPL type to quickly evaluate Scala expressions. When you add new expressions they are executed incrementally.
Notice that the Use class path of module option is disabled since the worksheet is a file that can you can share between other projects and should stay where you created it. You can change its location in the default settings, but it will be applied across the whole project and will affect other worksheets. If you need to change a class path, we recommend that you create and use a scratch file instead.
You can also select a compiler profile for your worksheet. Click the to access the Scala compiler settings for more options. Keep in mind that if you add/change a profile through the compiler settings page and you have an sbt project, your changes will be discarded on the next project import.
- For more options on the worksheet settings, select Worksheet tab. For example, on the Worksheet tab, you can set the output cutoff limit, specify in what mode you want to run the worksheet, or configure how you want IntelliJ IDEA to treat .sc and scratch files. | | | and click the
Debug Scala code
IntelliJ IDEA lets you debug your code using IntelliJ IDEA debugger or the sbt shell.
Debug Scala code using Intellij IDEA
Open your Scala application in the editor.
In the left gutter, set your breakpoints for the lines of code you want to debug. For more information on breakpoints, please see the Using Breakpoints topic.
If you need, you can access the Run/Debug configurations (Run | Edit Configurations) and adjust the settings, but usually the default settings are enough to successfully start and complete your debugging session.
Press Shift+F9. Alternatively, on the main toolbar, click the icon to start a debugging process.
- Evaluate the results in the Debug tool window.
For information on how to use options in the Debug tool window, please see Debug tool window references.
Debug Scala code using sbt shell
Open your sbt project.
Open your application in the editor.
- In the editor, in the left gutter, set your breakpoints for the lines of code you want to debug.
In the sbt projects tool window, click the icon, to start the sbt shell.
In the sbt shell tool window, click the icon to connect to the debugger server and start debugging session.
IntelliJ IDEA also creates a Run/Debug configuration for the debugging session. If you need, you can edit the run/debug configuration settings, but the default settings should be enough to successfully debug your code.Run your program. Evaluate your results in the Debug tool window.
Test Scala applications
IntelliJ IDEA lets you test your Scala applications using ScalaTest and Specs2. You can also set and run the test scopes.
Test Scala applications using Scala Test
- Open your project.
- If you have an sbt project, open the
build.sbt
file and specify the following dependencies for ScalaTest:
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.1"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"
Click icon in the sbt projects tool window to refresh your project or use the Auto-import option specified in the sbt settings to automatically refresh your project each time you make changes to
build.sbt
. If you have a regular Scala project, use the Project Structure dialog, to configure test libraries.
- If you have an sbt project, open the
Open a class in the editor, for which you want to create a test and place the cursor within the line containing the class declaration.
Press Ctrl+Shift+T and select Create New Test.
In the dialog that opens, specify your test settings and click OK.
Open the test in the editor, press Ctrl+Shift+F10 or right-click on the test class and from the context menu select Run 'test name'.
IntelliJ IDEA creates a run/debug configuration for the test automatically, but if you want to edit settings in your configuration, click
on the main menu.- In the Run/Debug Configurations dialog, on the right-hand side, specify settings for the test suite and click OK.
The configuration has standard options and you can find further details in the Performing Tests section. However, you can also specify the following Scala-related options:You can select the Use sbt shell checkbox to run your test via sbt shell and the Use UI with sbt to display the test results in the same format as for platform test runner.
You can select the Test kind option to specify what kind of test you want to run. For example, you can select Regular expression and set class and test patterns.
On the main toolbar, click the icon to run the test.
Evaluate the results in the Run tool window.
Test a Scala application using Specs2
The procedure for testing a Scala application using Specs2 is the same as the procedure described in the Test a Scala application using ScalaTest section except for the following options:
- You need to specify the following dependency for your sbt project:
libraryDependencies ++= Seq("org.specs2" %% "specs2-core" % "3.9.5" % "test")
For regular Scala projects, use the Project Structure dialog, to configure the test library.
The Use UI with sbt option that displays the test results in the same format as for platform test runner is not available.
Test scopes in Scala
IntelliJ IDEA lets you test scopes using ScalaTest or Specs2.
You can run tests inside a scope or test the whole scope in your Scala projects.
Create your code. Check the following example:
- In the editor, depending on your test scope you can perform the following:
If you open context menu for one of the tests inside the scope, you can create a run configuration just for the specified test inside that scope.
If you open a context menu for the whole scope, you can create a run configuration for all tests inside the scope.
Run your tests and view the output in the Run tool window. You can also use the sbt shell to run scope tests.
Run Scala tests with coverage
IntelliJ IDEA lets you run your test suite with code coverage.
Open your project.
Open the test in question in the editor.
- In the left gutter, click the icon and select the Run 'name' with Coverage option.
IntelliJ IDEA runs the test and displays the Coverage tool window with code coverage information.You can also adjust default code coverage settings or code coverage behavior if you need.
If you have already run you test suite, IntelliJ IDEA creates the run/debug configuration automatically. You can open the created run configuration and adjust the settings.
If you want to create a new run configuration for the test suite, do the following:From the main menu, select
.In the Run/Debug Configurations dialog, click the icon from the options on the left.
From the list that opens, select the configuration you need.
From the options on right, click the Code Coverage tab.
Adjust the default settings and click OK.
For more information, please see Configuring Code Coverage Measurement
You can also adjust code coverage behavior.
Press Ctrl+Alt+S to open the Settings/Preferences page.
From the options on the left, select
.From the options on right, adjust the settings and click OK.