Run unit tests in IntelliJ IDEA from multiple modules together

Wojtek Erbetowski picture Wojtek Erbetowski · Jul 13, 2012 · Viewed 38.2k times · Source

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.

Answer

Tomasz Kalkosiński picture Tomasz Kalkosiński · Jul 16, 2013

Best way way: (edit after 3 years)

There is even a better way to achieve this.

  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.

  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 prefere 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.