Filtering tests and test logging

A passing test is silent, which is right for a full build and wrong while one test is being written. Two settings of the test task change that: the logging events that print a line per test, and the filter that runs one test instead of the suite.

testLogging

settings.gradle

rootProject.name = 'shop'

build.gradle

plugins { id 'java' } repositories { mavenLocal() } dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.14.3' testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.14.3' } tasks.named('test') { useJUnitPlatform() testLogging { events 'passed', 'skipped', 'failed' } }

src/main/java/com/example/Calc.java

package com.example; public final class Calc { public static int add(int a, int b) { return a + b; } public static int divide(int a, int b) { if (b == 0) { throw new ArithmeticException("division by zero"); } return a / b; } }

src/test/java/com/example/CalcTest.java

package com.example; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; class CalcTest { @Test void adds() { assertEquals(4, Calc.add(2, 2)); } @Test void divides() { assertEquals(3, Calc.divide(9, 3)); } @Test void refusesToDivideByZero() { assertThrows(ArithmeticException.class, () -> Calc.divide(1, 0)); } @Test @Disabled("the rounding rule is not decided yet") void roundsHalfUp() { assertEquals(2, Calc.divide(5, 2)); } }

Every test named

gradle test --console=plain --offline --no-daemon

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses

> Task :test

CalcTest > adds() PASSED

CalcTest > roundsHalfUp() SKIPPED

CalcTest > refusesToDivideByZero() PASSED

CalcTest > divides() PASSED

BUILD SUCCESSFUL in 20s
3 actionable tasks: 3 executed

Three lines for three outcomes. PASSED is a test that ran and passed, SKIPPED is the one with @Disabled, and a failure would print FAILED with its reason below it. A skipped test is not a filtered test: it exists in the class file, the runner decided not to run it, and its reason is part of the report.

--tests

A filter selects tests by class or by method and leaves the rest out of the run:

gradle test --tests 'com.example.CalcTest.adds' --console=plain --offline --no-daemon

> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE

> Task :test

CalcTest > adds() PASSED

BUILD SUCCESSFUL in 15s
3 actionable tasks: 1 executed, 2 up-to-date

One line, and the other three tests are not skipped: they were never selected. That is why a filtered run is fast, and why it says nothing about the rest of the suite. In a CI job the filter belongs to the developer’s terminal, not to the pipeline.

Related

Article author: Arthur Isaev