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 system 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