Würgspaß
Würgspaß

Reputation: 4820

Cannot include/exclude Junit tests classes by Category using gradle

Given these interfaces

testclient.priority.High
testclient.priority.Low

and a junit classes annotated like this

@Category(testclient.priority.High.class)
public class MyTest {
...
}
@Category(testclient.priority.Low.class)
public class MyOtherTest {
...
}

I tried to configure an include/exclude pattern in build.gradle like this

useJUnit {
    includeCategories 'testclient.priority.High'
    excludeCategories 'testclient.priority.Low'
}

The problem is that no tests are run, at all. How is the exact syntax to achieve that? I am using gradle 2.14.1 and invoke the tests by "clean build".

Upvotes: 2

Views: 2199

Answers (2)

Würgspaß
Würgspaß

Reputation: 4820

I found the problem myself. I missed the fact that

High extends Low

so the exclusion correctly affected all annotated tests. The extension is intended and the correct pattern in build.gradle would simply be:

useJUnit {
    includeCategories 'testclient.priority.High'
}

In fact there are five priority levels and the extension dependency is an easy way to configure something like "run everything of this level or higher".

Upvotes: 1

Michael Easter
Michael Easter

Reputation: 24468

This works for me with Gradle 2.12. I don't know what is different between our respective environments, but I have placed my solution on GitHub for reference.

Note that I have placed FastTest.java and SlowTest.java in the src/test/java/net/codetojoy folder, where the tests reside.

Upvotes: 1

Related Questions