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.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

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

  1. Xxiii says:

    11 seconds to run a single test?

Leave a Reply


eight + 4 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books