Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom JUnit test detection using gradle

Tags:

java

junit

gradle

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

like image 990
Karl Walsh Avatar asked Feb 26 '26 02:02

Karl Walsh


1 Answers

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.

like image 114
David Pärsson Avatar answered Feb 27 '26 14:02

David Pärsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!