Search code examples
javaunit-testinggradlejunittest-suite

How to run Categorized Junit tests using Gradle?


My Question is similar to this question, but it refers to Gradle not Maven.

I have marked several tests within my project with the @Category annotation, and created my Test Suite (See below).

How do I run this using Gradle?

Test Suite:

@RunWith(Categories.class)
@Categories.IncludeCategory(MyTestSuite.class)
@Suite.SuiteClasses(ClassUnderTest.class)
public interface MyTestSuite {
}

Solution

  • I suppose, you need something like this, according to JUnit wiki:

    Gradle's test task allows the specification of the JUnit categories you want to include and exclude.

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

    Configuration above is example of global tests configuration, but you can create a custom test task to run specific test categories, as follows:

    task customTest(type: Test) {
        useJUnit {
            includeCategories 'some.package.name.MyTestSuite'
        }
    }