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.
Best way way: (edit after 3 years)
There is even a better way to achieve this.
Select menu "Run" → "Edit Configurations...". Click green plus in left top corner and select JUnit.
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.Select "Search for tests" to "In whole project". Working directory should be set to top module working directory (it should be set by default).
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.
- Select first module, right-click on
test/java
directory and "Run All Tests". It creates test configuration. - Select "Edit configurations" and check "Share" on newly created configuration so it will be saved.
- Select second module, "Run All Tests" on it, and check "Share" on this configuration as well.
- 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.
You have to create a "Run Configuration":
- Go to the dropdown on the top, at the right hand of the "Make" button and click on it
- Select "Edit Configurations"
- Now click on the "+" button to add a new run configuration and select JUnit
- Then, when configuring the "Run Configuration", you'll find a "Test Kind" dropdown, select "All classes in directory"
- 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.
- Select the IntelliJ module from where picking up the classpath (it can be the top level project if it has a classpath)
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:
The easiest way I found was to 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
Right click and choose to run all tests
Get a new run configuration
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.
'Development Tip' 카테고리의 다른 글
How to get the global object in JavaScript? (0) | 2020.10.21 |
---|---|
log messages appearing twice with Python Logging (0) | 2020.10.21 |
How to delete '#' sign in angular-ui-router URLs (0) | 2020.10.21 |
How to use the legacy Apache HTTP client on Android Marshmallow? (0) | 2020.10.21 |
Missing “/extras/google/google_play_services/libproject” folder after update to revision 30 (0) | 2020.10.21 |