About Hubert Ikkink

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in.

Gradle Goodness: Running a Single Test

We can run test code with Gradle using the test task that is added by the Java plugin. By default all tests found in the project are executed. If we want to run a single test we can use the Java system property test.single with the name of the test. Actually the pattern for the system property is taskName.single. The taskName is the name of the task of type Test in our project. We will see how we can use this in our builds.

First we create a simple build.gradle file to run our tests:
 
 
 
 

// File: build.gradle
apply plugin: 'java'
 
repositories {
    mavenCentral()
}
 
dependencies {
    testCompile 'junit:junit:[4,)'
}
 
test {
    testLogging {
        // Show that tests are run in the command-line output
        events 'started', 'passed'
    }
}

Next we create two test classes with each a single test method, just to demonstrate we can invoke them as single test later on.

// File: src/test/java/com/mrhaki/gradle/SampleTest.java
package com.mrhaki.gradle;
 
import static org.junit.Assert.*;
import org.junit.*;
 
public class SampleTest {
 
    @Test public void sample() {
        assertEquals("Gradle is gr8", "Gradle is gr8");
    }
    
}
// File: src/test/java/com/mrhaki/gradle/AnotherSampleTest.java
package com.mrhaki.gradle;
 
import static org.junit.Assert.*;
import org.junit.*;
 
public class AnotherSampleTest {
 
    @Test public void anotherSample() {
        assertEquals("Gradle is great", "Gradle is great");
    }
}

To only run the SampleTest we must invoke the test task from the command-line with the Java system property -Dtest.single=Sample:

$ gradle -Dtest.single=Sample test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
 
com.mrhaki.gradle.SampleTest > sample STARTED
 
com.mrhaki.gradle.SampleTest > sample PASSED
 
BUILD SUCCESSFUL
 
Total time: 11.404 secs

Notice only one test is execute now. Gradle will get the value Sample and uses it in the following pattern **/<Java system property value=Sample>*.class to find the test class. So we don’t have to type the full package and class name of our single test class. To only invoke the AnotherSampleTest test class we run the test task with a different value for the Java systme property:

$ gradle -Dtest.single=AnotherSample test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
 
com.mrhaki.gradle.AnotherSampleTest > anotherSample STARTED
 
com.mrhaki.gradle.AnotherSampleTest > anotherSample PASSED
 
BUILD SUCCESSFUL
 
Total time: 5.62 secs

We can also use a pattern for the Java system property to run multiple tests that apply to the pattern. For example we can use *Sample to run both SampleTest and AnotherSampleTest:

$ gradle -Dtest.single=*Sample test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test
 
com.mrhaki.gradle.AnotherSampleTest > anotherSample STARTED
 
com.mrhaki.gradle.AnotherSampleTest > anotherSample PASSED
 
com.mrhaki.gradle.SampleTest > sample STARTED
 
com.mrhaki.gradle.SampleTest > sample PASSED
 
BUILD SUCCESSFUL
 
Total time: 5.605 secs

To show the Java system property also works for other tasks of type Test we add a new task to our build.gradle file. We name the task sampleTest and include our tests. We also apply the same testLogging now to all tasks with type Test so we can see the output.

// File: build.gradle
apply plugin: 'java'
 
repositories {
    mavenCentral()
}
 
dependencies {
    testCompile 'junit:junit:[4,)'
}
 
task sampleTest(type: Test, dependsOn: testClasses) {
    include '**/*Sample*'
}
 
tasks.withType(Test) {
    testLogging {
        events 'started', 'passed'
    }
}

Next we want to run only the SampleTest class, but now we use the Java system property -DsampleTest.single=S*:

$ gradle -DsampleTest.single=S* sampleTest
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:sampleTest
 
com.mrhaki.gradle.SampleTest > sample STARTED
 
com.mrhaki.gradle.SampleTest > sample PASSED
 
BUILD SUCCESSFUL
 
Total time: 10.677 secs

Code written with Gradle 1.6
 

Reference: Gradle Goodness: Running a Single Test from our JCG partner Hubert Ikkink at the JDriven blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Gradle Goodness: Running a Single Test"

  1. Xxiii says:

    11 seconds to run a single test?

Leave a Reply


4 − three =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close