DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How are you handling the data revolution? We want your take on what's real, what's hype, and what's next in the world of data engineering.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • Externalize Microservice Configuration With Spring Cloud Config
  • Replace your Scripts with Gradle Tasks

Trending

  • Streamlining DevOps: How Containers and Kubernetes Deliver
  • A Beginner’s Guide to Playwright: End-to-End Testing Made Easy
  • Are Traditional Data Warehouses Being Devoured by Agentic AI?
  • Serverless Machine Learning: Running AI Models Without Managing Infrastructure
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Gradle Goodness: Running a Single Test

Gradle Goodness: Running a Single Test

Learn how to run test code with Gradle using the test task that is added by the Java plugin.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
May. 17, 13 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
116.5K Views

Join the DZone community and get the full member experience.

Join For Free

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 theAnotherSampleTest 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 bothSampleTest 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 tasksampleTest 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

Testing Gradle Task (computing) Property (programming)

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • Externalize Microservice Configuration With Spring Cloud Config
  • Replace your Scripts with Gradle Tasks

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: