Main Tutorials

Gradle – How to display test result in the console

By default, gradle test displays only the test summary.

Terminal

% gradle test

BUILD SUCCESSFUL in 4s
6 actionable tasks: 3 executed, 3 up-to-date

P.S Gradle test generates the detailed tests’ result at the build/reports/tests/test/index.html page.

P.S Tested with Gradle 6.7.1

1. Display test result in the console.

1.1 We add the testLogging events to tell Gradle what kind of test events should display in the console.

build.gradle

test {
	testLogging {
		events "PASSED", "SKIPPED", "FAILED", "STANDARD_OUT", "STANDARD_ERROR"
	}
}

1.2 Review the TestLogEvent, it consist of 6 events:

  1. FAILED – A test has failed.
  2. PASSED – A test has passed.
  3. SKIPPED – A test has been skipped.
  4. STANDARD_ERROR – A test has written a message to standard error.
  5. STANDARD_OUT – A test has written a message to standard out.
  6. STARTED – A test has started.

Generally, the following 3 events should be enough for most use cases.

build.gradle

test {
	testLogging {
		events "PASSED", "SKIPPED", "FAILED"
	}
}

1.3 For examples:

Terminal

% gradle test

DummyTest > test_a_ok() FAILED
    org.opentest4j.AssertionFailedError at DummyTest.java:11

DummyTest > test_b_ok() PASSED

LogParserTest > test_invalid_request_ok() PASSED

BUILD SUCCESSFUL in 3s

2. DEMO

2.1 Review a JUnit 5 @ParameterizedTest unit tests to test the bad user agents.

BadUserAgentSecurityRuleTest.java

package com.mkyong.security.rule;

import com.mkyong.security.log.LogLine;
import com.mkyong.security.rules.SecurityRule;
import com.mkyong.security.rules.SecurityRuleID;
import com.mkyong.security.rules.ValidateResult;
import com.mkyong.security.rules.impl.BadUserAgentSecurityRuleImpl;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class BadUserAgentSecurityRuleTest {

    private static final SecurityRule RULE = new BadUserAgentSecurityRuleImpl();

    @ParameterizedTest(name = "#{index} - Bad User Agent={0}")
    @ValueSource(strings = {
            "python", "java", "scrapy", "go-http", "HTTrack", "WebCopy",
            "PYTHON", "Java", "SCrapy", "Go-http", "httrack", "WEBCopy",
            "httpclient", "wget", "curl",
            "Apache-HttpClient/4.3.6 (java 1.5)",
            "Scrapy/2.3.0 (+https://scrapy.org)",
            "python-requests/2.24.0",
            "p2/1.1.201.v20161115-1927 (Java 1.8.0_121-b13)",
            "-",
            "",
            " ",
            "  "
    })
    void test_bad_user_agent(String userAgent) {

        LogLine logLine = new LogLine();
        logLine.setUserAgent(userAgent);

        ValidateResult result = RULE.isValid(logLine);
        assertEquals(false, result.isValid());
        assertEquals(SecurityRuleID.BAD_USER_AGENT, result.getSecurityRuleIds());
        assertEquals(userAgent, result.getTest());
        assertTrue(result.getMatches().size() >= 1);

    }

}

2.2 We add below content to the build.gradle file.

build.gradle

test {
	testLogging {
		events "PASSED", "SKIPPED", "FAILED"
	}
}

2.3 Run tests from a single unit test class BadUserAgentSecurityRuleTest.

Terminal

% gradle cleanTest test --tests BadUserAgentSecurityRuleTest

> Task :test

BadUserAgentSecurityRuleTest > #1 - Bad User Agent=python PASSED

BadUserAgentSecurityRuleTest > #2 - Bad User Agent=java PASSED

BadUserAgentSecurityRuleTest > #3 - Bad User Agent=scrapy PASSED

BadUserAgentSecurityRuleTest > #4 - Bad User Agent=go-http PASSED

BadUserAgentSecurityRuleTest > #5 - Bad User Agent=HTTrack PASSED

BadUserAgentSecurityRuleTest > #6 - Bad User Agent=WebCopy PASSED

BadUserAgentSecurityRuleTest > #7 - Bad User Agent=PYTHON PASSED

BadUserAgentSecurityRuleTest > #8 - Bad User Agent=Java PASSED

BadUserAgentSecurityRuleTest > #9 - Bad User Agent=SCrapy PASSED

BadUserAgentSecurityRuleTest > #10 - Bad User Agent=Go-http PASSED

BadUserAgentSecurityRuleTest > #11 - Bad User Agent=httrack PASSED

BadUserAgentSecurityRuleTest > #12 - Bad User Agent=WEBCopy PASSED

BadUserAgentSecurityRuleTest > #13 - Bad User Agent=httpclient PASSED

BadUserAgentSecurityRuleTest > #14 - Bad User Agent=wget PASSED

BadUserAgentSecurityRuleTest > #15 - Bad User Agent=curl PASSED

BadUserAgentSecurityRuleTest > #16 - Bad User Agent=Apache-HttpClient/4.3.6 (java 1.5) PASSED

BadUserAgentSecurityRuleTest > #17 - Bad User Agent=Scrapy/2.3.0 (+https://scrapy.org) PASSED

BadUserAgentSecurityRuleTest > #18 - Bad User Agent=python-requests/2.24.0 PASSED

BadUserAgentSecurityRuleTest > #19 - Bad User Agent=p2/1.1.201.v20161115-1927 (Java 1.8.0_121-b13) PASSED

BadUserAgentSecurityRuleTest > #20 - Bad User Agent=- PASSED

BadUserAgentSecurityRuleTest > #21 - Bad User Agent= PASSED

BadUserAgentSecurityRuleTest > #22 - Bad User Agent=  PASSED

BadUserAgentSecurityRuleTest > #23 - Bad User Agent=   PASSED

BUILD SUCCESSFUL in 2s
7 actionable tasks: 4 executed, 3 up-to-date

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Paul
3 years ago

Is there an equivalent way to control logging when NOT testing?