gradle 不同类别的 junit 类别附加测试任务

gradle junit category additional testtask with different categories

基于How to specify @category in test task in gradle?

我想要 2 个不同的测试任务:

我已经成功地修改了构建任务 test,使 "SlowTest"-s 被允许:

org.namespace.some.MySlowTestClass#someReallyLongRunningTest在执行任务"test"

时没有按要求执行

我的问题:是否可以添加一个额外的 gradle 任务 "testLongRunning" 来执行所有测试(包括 org.namespace.some.MySlowTestClass#someReallyLongRunningTest),而 gradle 任务 "test"不执行慢的?

我跳过的工作示例 SlowTest 看起来像这样:


// subproject/build.gradle   
apply plugin: 'java'

dependencies {
    testCompile 'junit:junit:4.11'
}

test {
    useJUnit {
        excludeCategories 'org.junit.SlowTest' // userdefined interface in "subproject/src/test/java/org/junit/SlowTest.java"
    }
}

// subproject/src/test/java/org/junit/SlowTest.java
package org.junit;

// @Category see 
public interface SlowTest {
 /* category marker for junit 
    via @Category(org.junit.SlowTest.class) */ 
}

// subproject/src/test/org/namespace/some/MySlowTestClass.java
package org.namespace.some;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.junit.experimental.categories.*;

public class MySlowTestClass {

    // @Category see 
    @Category(org.junit.SlowTest.class)
    @Test
    public void someReallyLongRunningTest(){
    }
}

我尝试过的:

当我将它添加到 subproject/build.gradle

// this is line 65
task testLongRunning (type: test){
    dependsOn test
    useJUnit {
        includeCategories 'org.junit.SlowTest'
    }
}

我收到这个错误

FAILURE: Build failed with an exception.

* Where:
Build file '...\subproject\build.gradle' line: 66

* What went wrong:
A problem occurred evaluating project ':subproject'.
> org.gradle.api.tasks.testing.Test_Decorated cannot be cast to java.lang.Class

您的类型似乎有误。尝试将 (type: test) 更改为 (type: Test)。我认为 dependsOn test 试图向我们提供 test 您作为类型传递的信息,而不是将其视为实际任务。

遇到了同样的问题。以下对我有用(类似于 Tim VanDoren 的建议):

test {
    useJUnit {
        includeCategories 'com.common.testing.UnitTest'
    }
}

task integrationTest (type: Test) { // Use 'Test' instead of 'test' here
    dependsOn test
    useJUnit {
        includeCategories 'com.common.testing.IntegrationTest'
    }
}