2

Based on How to specify @category in test task in gradle?

I want to have 2 different test tasks:

  • test for the fast tests
  • testLongRunning for the fast and the slow tests.

I have successfully modified the buildin task test in a way that the "SlowTest"-s are onmitted:

org.namespace.some.MySlowTestClass#someReallyLongRunningTest is not executed as desired when executing task "test"

My question: Is it possible to add an additional gradle task "testLongRunning" that executes all tests (including org.namespace.some.MySlowTestClass#someReallyLongRunningTest) while the gradle task "test" does not excute the slow one?

My working example thas skips SlowTestlooks like this:


// 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(){
    }
}

what i have tried:

when i add this to subproject/build.gradle

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

i get this error

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
Community
  • 1
  • 1
k3b
  • 14,517
  • 7
  • 53
  • 85

2 Answers2

3

It looks like your type might be incorrect. Try changing (type: test) to (type: Test). I think dependsOn test is trying to us the test you're passing in as the type rather than seeing it as the actual task.

2

Faced the same issue. The following worked for me (similar to what Tim VanDoren suggested):

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'
    }
}
Vladimir Salin
  • 2,951
  • 2
  • 36
  • 49