2016-08-10 32 views
1

gradle這個JUnit的類別與不同類別的附加testtask基於​​</p> <p>我想有2個不同的測試任務

  • test爲快速測試
  • testLongRunning的快和慢的測試。

我已成功修改了的buildin任務test的方式,將「SlowTest」 -s是onmitted:

org.namespace.some.MySlowTestClass#someReallyLongRunningTest不執行爲執行任務「測試」

我的問題時所需的:是否有可能添加一個額外的gradle任務「testLongRunning」執行所有測試(包括org.namespace.some.MySlowTestClass#someReallyLongRunningTest),而gradle任務「測試」不執行慢的任務?

我的工作例如全髖關節置換跳過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(){ 
    } 
} 

我曾嘗試:

當我加入這子項目/的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

它看起來像你的類型可能不正確。嘗試更改(type: test)(type: Test)。我認爲dependsOn test正在嘗試將您傳入的test作爲類型,而不是將其視爲實際任務。

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' 
    } 
}