The test reports
The test task writes two reports for one run: an HTML page for a person and a set of XML files for a machine. Both live under build/, both are rebuilt on every run, and both say more than the log does.
A project with three outcomes
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()
}
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;
}
}
src/test/java/com/example/CalcTest.java
package com.example;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class CalcTest {
@Test
void adds() {
assertEquals(4, Calc.add(2, 2));
}
@Test
@Disabled("waiting for a decision")
void addsBigNumbers() {
assertEquals(4, Calc.add(2, 2));
}
}
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
BUILD SUCCESSFUL in 17s
3 actionable tasks: 3 executed
The two folders
find build/reports/tests build/test-results -type f | sort
build/reports/tests/test/com.example.CalcTest/addsBigNumbers().html
build/reports/tests/test/com.example.CalcTest/index.html
build/reports/tests/test/css/base-style.css
build/reports/tests/test/css/style.css
build/reports/tests/test/index.html
build/reports/tests/test/js/report.js
build/test-results/test/TEST-com.example.CalcTest.xml
build/test-results/test/binary/output-events.bin
build/test-results/test/binary/results-generic.bin
build/reports/tests/test/index.html is the page to open: it lists the classes, the tests, the time each one took and the reason of a skip or a failure, and it links the standard output of every test. The css/ and js/ folders beside it are what makes the page work without a network.
build/test-results/test/ holds one XML per test class, with a TEST- prefix, and a binary/ folder with the raw results Gradle keeps for its own up-to-date check. A CI job publishes the XML, because every CI service reads JUnit XML; nobody publishes the binary folder.
The XML a CI job reads
grep -o 'tests="[0-9]*" skipped="[0-9]*" failures="[0-9]*" errors="[0-9]*"' build/test-results/test/TEST-com.example.CalcTest.xml
tests="2" skipped="1" failures="0" errors="0"
The four counts of the class: two tests, one of them skipped, none failed and none errored. The same element also carries a timestamp, the host name and the time the class took, which change on every run and are left out of the line above. A pipeline reads the counts instead of parsing the log, and a dashboard draws a history from the timestamps.
Related
- A failing test: the report path the failure points at.
- Filtering tests and test logging: the per-test lines of the log.
- Surefire and JUnit in Maven: the same two kinds of report in target/surefire-reports.
Article author: Arthur Isaev