Gradle – How to specify @category in test task in gradle

gradlejunit

I would like to learn whether I can specify the @category in gradle test task. So, I can run separately integration junits and the normal junit tests.

I know that there is currently no explicit support for @category in gradle and I am looking for a workaround for this limitation.

The straightforward approach is to define test suits for different test categories, e.g., How to run all tests belonging to a certain Category in JUnit 4. I would prefer to specify @category in my gradle script.

Best Answer

Support for including or excluding categories has been included in gradle:

test {
  useJUnit {
    includeCategories 'org.gradle.junit.CategoryA'
    excludeCategories 'org.gradle.junit.CategoryB'
  }
}

Full documentation: https://docs.gradle.org/current/userguide/java_testing.html#test_grouping