IntelliJ IDEA 2019.1 Help

Work with tests in Gradle

IntelliJ IDEA lets you use different test runners to execute tests in a Gradle project.

You can use JUnit to run your tests. In this case tests are run much faster due to the incremental compilation. You can delegate the testing process to Gradle. In this case when you run your tests, you will get the same results on the continuous integration (CI) server. Also, no matter how difficult your Gradle project is, tests that are run in command line will always work in IDE. You can also decide which test runner to use specifically per each test.

Configure and use a Gradle test runner

  1. In the Gradle projects tool window, click icons general settings svg.

  2. In the Settings dialog, right-click Gradle and from the list, select Runner.

  3. On the page that opens, in the Run test using list, select an option that you want to use for your test run.

    gradle test runner
    (The default value is Platform Test Runner.)

  4. Click OK.

  5. In your Gradle project, in the editor, create or select a test that you want to run.

  6. From the context menu, select Run <test name>.
    Alternatively, click the icons toolwindows toolWindowRun svg icon in the left gutter.

    gradle run test gutter

  7. Depending on what you chose as your test runner, IntelliJ IDEA runs your tests in one of the following ways:
    • If you selected Platform Test Runner, IntelliJ IDEA runs tests using a JUnit test runner and displays the output in the Run JUnit tool window.

      platform test runner output

    • If you selected Gradle Test Runner, IntelliJ IDEA runs tests using a Gradle test runner and displays the output in the Run Gradle tool window.
      gradle test runner output

      While in the Run Gradle tool window, you can click the open gradle test report icon icon to get the Gradle test report.

      You can also click the import test results icon to check the previously saved tests' results.

    • You can also select Run 'name()' with coverage to see the code coverage for your test.

      gradle test coverage
      It works for the platform test runner as well as for the Gradle test runner.

    • If you selected Let me choose per test, IntelliJ IDEA lets you choose between JUnit or Gradle to run your test.

      gradle choose per test
      Once you have selected the test runner, IntelliJ IDEA will remember your selection and automatically will run your test using the option you've chosen.

    You can change your selection, in the Runner settings page.

Debug Gradle tests

  1. In your Gradle project, in the editor, create or select a test that you want to debug.

  2. From the context menu, select Debug <test name>.

  3. If the Let me choose per test option is selected in the Runner settings, product% clears the Enable Gradle script debugging option located inside the Run/debug configuration and disables the breakpoints in Gradle scripts.

    Enable Gradle script debugging
    This is done to speed up your debugging process. You can manually enable or disable breakpoints in the Gradle scripts by selecting or clearing the Enable Gradle script debugging option.

Run internal Gradle test suits

Sometimes, in the multi-module projects you might want to see information about Gradle internal tests suites. For example, when tests are run in parallel, you can make IntelliJ IDEA display information about the tests processes. IntelliJ IDEA also displays how many of those processes are working simultaneously.

  1. In the Run tool window, click the Settings icon and select Show internal Gradle test suits.

    Run tool window

  2. Rerun the tests to see the test results that include the Gradle internal ones.

Last modified: 20 June 2019