如何使用 Gradle 运行 分类 Junit 测试?

How to run Categorized Junit tests using Gradle?

我的问题类似于 this question,但它指的是 Gradle 而不是 Maven

我在我的项目中用 @Category 注释标记了几个测试,并创建了我的测试套件(见下文)。

如何使用 Gradle 运行?

测试套件:

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

我想,根据 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'
    }
}

以上配置为全局测试配置示例,但您可以针对运行特定测试类别创建自定义测试任务,如下:

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