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
Please enter at least three characters to search
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How to Test Gradle Plugins
  • Page Object Model for Performance Testing With Gatling
  • Test Driven Development Using TestNG, Gradle
  • Modern Test Automation With AI (LLM) and Playwright MCP

Trending

  • Unlocking the Benefits of a Private API in AWS API Gateway
  • Introduction to Retrieval Augmented Generation (RAG)
  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Non-Project Backlog Management for Software Engineering Teams
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Gradle Goodness: Show Standard Out or Error Output from Tests

Gradle Goodness: Show Standard Out or Error Output from Tests

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Oct. 18, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
14.4K Views

Join the DZone community and get the full member experience.

Join For Free

We use the Test task in Gradle to run tests. If we use the System.out.println or System.err.println methods in our test we don't see the output when we execute the tests. We can customize the test task to show any output send to standard out or error in the Gradle output.

First we show our test class written with Spock, but it could also be a JUnit or TestNG test:

// File: src/test/groovy/com/mrhaki/gradle/SampleSpec.groovy
package com.mrhaki.gradle

import spock.lang.*

class SampleSpec extends Specification {

    def "check that Gradle is Gr8"() {
        when:
        def value = 'Gradle is great!'

        then:
        // Include a println statement, so
        // we have output to show.
        println "Value = [$value]"
        value == 'Gradle is great!'
    }

}

Now we write a simple Gradle build file which can execute our test:

// File: build.gradle
apply plugin: 'groovy' // Adds test task

repositories.jcenter()

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.7'
    testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
}

Let's run the test task from the command line and look at the output:

$ gradle test
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test
 
BUILD SUCCESSFUL
 
Total time: 7.022 secs
$

Well at least our test is successful, but we don't see the output of our println method invocation in the test. We customize the test task and add thetestLogging method with a configuration closure. In the closure we set the property showStandardStreams to the value true. Alternatively we can set the events property or use the events method with the values standard_out and standard_err to achieve the same result. In the next build file we use the showStandardStreams property:

view sourceprint?
00.// File: build.gradle
01.apply plugin: 'groovy' // Adds test task
02.
03.repositories.jcenter()
04.
05.dependencies {
06.compile 'org.codehaus.groovy:groovy-all:2.3.7'
07.testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
08.}
09.
10.test {
11.testLogging {
12.// Make sure output from
13.// standard out or error is shown
14.// in Gradle output.
15.showStandardStreams = true
16.
17.// Or we use events method:
18.// events 'standard_out', 'standard_error'
19.
20.// Or set property events:
21.// events = ['standard_out', 'standard_error']
22.
23.// Instead of string values we can
24.// use enum values:
25.// events org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_OUT,
26.//  org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_ERROR,
27.}
28.}

We re-run the test task from the command line and look at the output to see the result from the println method:

$ gradle test
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test
com.mrhaki.gradle.SampleSpec > check that Gradle is Gr8 STANDARD_OUT
Value = [Gradle is great!]
BUILD SUCCESSFUL
Total time: 8.716 secs
$

Written with Gradle 2.1.

Testing Gradle

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

  • How to Test Gradle Plugins
  • Page Object Model for Performance Testing With Gatling
  • Test Driven Development Using TestNG, Gradle
  • Modern Test Automation With AI (LLM) and Playwright MCP

Partner Resources

×

Comments
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!