Search code examples
javaunit-testingintellij-ideagradleintegration-testing

How can intelliJ run gradle task as test


I have a gradle project with "unit test" and "integration tests" tasks defined as follows:

test {
  include '**/*Test.class'
}

task integrationTest(type: Test) {
  include '**/*IT.class'
}

I created a run configuration in IntelliJ to run all unit tests like image shows:

Run configuration of gradle 'test' task in intelliJ

And did the same with the task 'integrationTest':

Run configuration of gradle 'test' task in intelliJ

IntelliJ 'understands' the test task and run it showing graphical results, like in this image:

IntelliJ showing the results of gradle test task

The same doesn't happen when it runs the 'integrationTest' task. The results are shown in text, like when I run the task by command line.


Solution

  • Answering my own question... As far as I know you can't make IntelliJ to run tests of a specific task and the Pattern solution doesn't work so well.

    So, the only way I found to effectively separate integration tests in IntelliJ was with the use of a JUnit Category.

    1. Create an interface to represent integration tests. For example:

      public interface IntegrationTest {
      }
      
    2. You have to annotate every integration test class with the category annotation and the created interface:

      import org.junit.experimental.categories.Category;
      import mycompany.mypackage.IntegrationTest;
      
      @Category(IntegrationTest.class)
      public class DbfFileProcessorIT {
          ...
      }
      
    3. Create a build configuration filtering with Category: enter image description here