在 IntelliJIDEA 中从多个模块一起运行单元测试

如何一次运行来自两个或多个 IDEA 模块的所有测试?

我使用了很多模块,经常运行所有的单元测试很重要,当我选择运行多个文件夹时,上下文菜单上就没有“运行”选项了。

51328 次浏览

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)

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.

For me both solutions didn't work or didn't work es expected.

For me I have 3 modules. My modules dependencies look like this

Presentation -> Domain -> Data

I just wanted to execute all tests together. So I came up with the 'Across module dependencies' approach

My Settings look like this:

enter image description here

Another not so obvious case is when code coverage is needed on more than one project. The naive solution would be to select multiple projects and run all unit tests in them at once. As it turns out, unit tests may fail if the classpath changes and IntelliJ has exactly one classpath entry per run configuration. In this case, running unit tests on projects sequentially is actually sufficient. That's because at the end of each run IntelliJ (2017.2.5 Community Edition) asks if the collected coverage should replace or should be added to previously collected coverage stats.

This worked for me for a project with multiple modules.

Create a new JUnit run/debug configuration. Test kind: 'All in package' Search for tests: 'Whole project'

You will have to specify a working directory as well.

Select all modules, right-click them and choose to run all tests. This will create a configuration called "Whole Project" which you can run again at any time.

I found this better than the accepted answer because this runs the unit tests separately for each module. If your test cases use module-specific resources during its run-time then the accepted answer's best way won't work.

Select all modules

Select all modules

Right click and choose to run all tests

Right click and choose to run all tests

Get a new run configuration

Get a new run configuration

If you use Gradle and have multiple modules, you can do it that way:

  • make sure IntelliJ is set to use Gradle to run the tests (Settings/Build, Execution, Deployment/Build Tools/Gradle -> Run tests using: Gradle)
  • right click over a module in the Project navigator and select Run test in [module name]
  • once done, left click on the configuration that was created in the top toolbar and select Edit Configurations...
  • in the Run field, simply add more modules just after the first one, for example, if you want to run tests in modules foo, bar and baz: :foo:test :bar:test :baz:test --tests *
  • rename the configuration, for example Test Foo + Bar + Baz

Now you can just run the configuration to perform the tests you need.