3

Our test suite is growing quickly and we have reached a point where our more functional tests are dependent on other systems.

We use gradle test tasks to run these tests using the include and exclude filters but this is becoming cumbersome because we are having to name our tests in a particular way.

Our current approach is to name our tests in the following way:

class AppleSingleServiceTest {}
class BananaMultiServiceTest {}
class KiwiIntegrationTest {}

and then include tests in the relevant task using

include '**/*SingleServiceTest.class'
include '**/*MultiServiceTest.class'
include '**/*IntegrationTest.class'

Is it possible find test classes in gradle by looking at annotations?

@SingleServiceTest
public class AppleTest {}

I think any tests that are not annotated would then be run as normal unit tests, so if you forget to annotate a more functional test it will fail

An example of a single service test is a selenium test where any external dependencies of the SUT are stubbed

An example of a multi service test is one where some but maybe not all external dependencies are not stubbed

Karl Walsh
  • 630
  • 7
  • 15
  • 2
    While it's not exactly what you're asking for, I think this problem can be solved by placing different kinds of tests in different folders. That's how we're doing it. JUnit supports doing this using the [`@Category` annotation](http://stackoverflow.com/questions/2176570/how-to-run-all-tests-belonging-to-a-certain-category-in-junit-4). It's [not yet supported by gradle](http://issues.gradle.org/browse/GRADLE-2111) but you might be able to work something out. – David Pärsson Nov 30 '12 at 12:52
  • 1
    `@Category` is meant to be used together with a JUnit suite. (Yes I know it's cumbersome). To my knowledge, JUnit doesn't provide any other means to filter by categories. Of course Gradle could implement its own annotation-based filtering approach, similar to what Maven does. – Peter Niederwieser Nov 30 '12 at 12:55
  • @David It's a possibility and we might actually try it; My preference is to indicate what kind of test it is with some meta info (annotations) and leave the location of the tests to the developer (we currently use the maven-style project structure) – Karl Walsh Nov 30 '12 at 12:58
  • @PeterNiederwieser Perhaps a custom gradle plugin could do this? It's something I have been thinking about but would be starting from scratch with no idea if what I want to achieve is possible at the moment. – Karl Walsh Nov 30 '12 at 12:59
  • It would be better to enhance the class file scanning performed by the `Test` task, rather than developing a new solution from scratch. – Peter Niederwieser Nov 30 '12 at 15:06

3 Answers3

5

As of Gradle 1.6, Gradle supports selecting tests with JUnit @Category, like this:

test {
    useJUnit {
        includeCategories 'org.gradle.junit.CategoryA'
        excludeCategories 'org.gradle.junit.CategoryB'
    }
}

More details can be found in the docs.

David Pärsson
  • 6,038
  • 2
  • 37
  • 52
1

The feature you are asking for doesn't currently exist, but you can make a feature request at http://forums.gradle.org. Or you can use the (cumbersome) JUnit @Category, which requires you to define test suites.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
0

I had a similar need to filter tests with annotations. I eventually managed to create a solution. It is posted here.

Community
  • 1
  • 1
JBT
  • 8,498
  • 18
  • 65
  • 104