gradle junit类别附加不同类别的testtask

时间:2016-08-10 14:49:55

标签: java gradle junit task categories

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

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

  • test用于快速测试
  • testLongRunning用于快速和慢速测试。

我已成功修改了buildin任务test,其方式是“SlowTest”-s被提交:

执行任务“test”时,

org.namespace.some.MySlowTestClass#someReallyLongRunningTest未按要求执行

我的问题:是否可以添加额外的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 https://stackoverflow.com/questions/38872369/cannot-include-exclude-junit-tests-classes-by-category-using-gradle
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 https://stackoverflow.com/questions/38872369/cannot-include-exclude-junit-tests-classes-by-category-using-gradle
    @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

2 个答案:

答案 0 :(得分:2)

看起来您的类型可能不正确。尝试将(type: test)更改为(type: Test)。我认为dependsOn test正在尝试将test作为类型传入,而不是将其视为实际任务。

答案 1 :(得分:1)

面临同样的问题。以下对我有用(类似于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'
    }
}