Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run unit tests in IntelliJ IDEA from multiple modules together

How can I run all tests from two or more IDEA modules at once?

I'm using many modules and it is important to run all of the unit tests often and when I choose more than one folder to run, there's no 'run' option on the context menu any more.

like image 538
Wojtek Erbetowski Avatar asked Jul 13 '12 10:07

Wojtek Erbetowski


People also ask

Does IntelliJ run tests in parallel?

Answering late for posterity. You can make JUnit tests run in parallel (or serial) to any level of granularity in IntelliJ by changing the Fork mode in the test's run configuration. Be careful not to confuse this with the Allow parallel run option, which lets you start the test execution multiple times within your IDE.

How do I open a multi module project in IntelliJ?

Import an existing moduleFrom the main menu, select File | New | Module from Existing Sources. In the dialog that opens, specify the path the . iml file of the module that you want to import, and click Open. By doing so, you are attaching another module to the project without physically moving any files.


2 Answers

Best way way: (edit after 3 years)

There is even a better way to achieve this. From the JetBrains JUnit Run Configuration docs:

  1. Select menu "Run" → "Edit Configurations...". Click green plus in left top corner and select JUnit.

  2. Select "Test kind" to "Pattern" and enter this regexp exactly as you see it: ^(?!.*IT$).*$ (it starts with caret ^ and ends with dollar $). This regexp says: all tests that do not finish with IT in their name.

    Note: The regexp will match against the qualified file names, making it easy to exclude by module/packages as well. If your integration tests are grouped in a package com.me.integrationtests, the regex to match everything not in this package would be ^(?!.*com\.me\.integrationtests.*).*$.

  3. Select "Search for tests" to "In whole project". Working directory should be set to top module working directory (it should be set by default).

  4. Enter a Name for your test like "All Unit tests". I also prefer to mark "Share" option so this configuration won't disappear later. Click Apply and OK.

You can experiment with this regexp to fit your needs.

Original answer:

It is doable, although it's not comfortable.

  1. Select first module, right-click on test/java directory and "Run All Tests". It creates test configuration.
  2. Select "Edit configurations" and check "Share" on newly created configuration so it will be saved.
  3. Select second module, "Run All Tests" on it, and check "Share" on this configuration as well.
  4. In "Before launch" section, click "+" and select "Run Another Configuration" and then select first module's configuration.

This way you run configurations in a sequence and every configuration gets a new tab. Still, better than nothing.

like image 80
Tomasz Kalkosiński Avatar answered Sep 24 '22 02:09

Tomasz Kalkosiński


You have to create a "Run Configuration":

  1. Go to the dropdown on the top, at the right hand of the "Make" button and click on it
  2. Select "Edit Configurations"
  3. Now click on the "+" button to add a new run configuration and select JUnit
  4. Then, when configuring the "Run Configuration", you'll find a "Test Kind" dropdown, select "All classes in directory"
  5. Select the directory you want to use as the root, you can choose the top level directory for your project or any of the directories for your modules.
  6. Select the IntelliJ module from where picking up the classpath (it can be the top level project if it has a classpath)
like image 37
Alonso Dominguez Avatar answered Sep 24 '22 02:09

Alonso Dominguez