Skip to content

Commit 3f9fd44

Browse files
author
Farid Zakaria
committed
Add support for @afterall in XML report
Writing a simple test like ``` package com.library; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Test; public class AlwaysFailTest { @test public void doNothingTest() { System.out.println("Hi there!"); } @afterall public static void alwaysFail() { throw new RuntimeException("Always failing."); } } ``` Produces an XML that seems to look like it succeeds. ```xml <?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name="com.library.AlwaysFailTest" timestamp="2024-10-10T21:19:11.522176Z" hostname="KHK9NLVQGN" tests="2" failures="0" errors="0" disabled="0" skipped="0" package=""> <properties/> <testcase name="doNothingTest" classname="com.library.AlwaysFailTest" time="0.03"> <system-out><![CDATA[Hi there! ]]></system-out> </testcase> </testsuite> </testsuites> ``` Bazel itself correctly identifies it fails. The Junit4 reporter also included with Bazel natively correclty reports the failure in the XML output. Augment the Junit listener to add support for static methods and add them to the JUnit output. Doing so produces the following XML. ```xml <?xml version="1.0" encoding="UTF-8"?> <testsuites> <testsuite name="com.library.AlwaysFailTest" timestamp="2024-10-10T21:49:02.096648Z" hostname="KHK9NLVQGN" tests="3" failures="1" errors="0" disabled="0" skipped="0" package=""> <properties/> <testcase name="com.library.AlwaysFailTest" classname="com.library.AlwaysFailTest" time="0.05"> <failure message="Always failing." type="java.lang.RuntimeException"><![CDATA[java.lang.RuntimeException: Always failing. at com.library.AlwaysFailTest.alwaysFail(AlwaysFailTest.java:20) ... at com.github.bazel_contrib.contrib_rules_jvm.junit5.JUnit5Runner.main(JUnit5Runner.java:39) ]]></failure> </testcase> <testcase name="doNothingTest" classname="com.library.AlwaysFailTest" time="0.03"> <system-out><![CDATA[Hi there! ]]></system-out> </testcase> </testsuite> </testsuites> ```
1 parent 43d61cb commit 3f9fd44

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/BazelJUnitOutputListener.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ private Map<TestData, List<TestData>> matchTestCasesToSuites_locked(
108108
// In all cases we're looking for the "class" segment, pull the UniqueID and map that from the
109109
// results
110110
List<UniqueId.Segment> segments = testCase.getId().getUniqueIdObject().getSegments();
111-
112-
if (segments.size() == 3 || segments.size() == 5) {
111+
112+
if (segments.size() == 2) {
113+
parent = results.get(testCase.getId().getUniqueIdObject());
114+
}
115+
else if (segments.size() == 3 || segments.size() == 5) {
113116
// get class / test data up one level
114117
parent =
115118
testCase
@@ -161,7 +164,7 @@ private List<TestData> findTestCases_locked() {
161164
// are identified by the fact that they have no child test cases in the
162165
// test plan, or they are marked as tests.
163166
TestIdentifier id = result.getId();
164-
return id.isTest() || testPlan.getChildren(id).isEmpty();
167+
return id.getSource() != null || id.isTest() || testPlan.getChildren(id).isEmpty();
165168
})
166169
.collect(Collectors.toList());
167170
}

java/src/com/github/bazel_contrib/contrib_rules_jvm/junit5/TestData.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,17 @@ public boolean isDynamic() {
146146
public Instant getStarted() {
147147
return this.started;
148148
}
149+
150+
@Override
151+
public String toString() {
152+
return "TestData{" +
153+
"id=" + id +
154+
", reportEntries=" + reportEntries +
155+
", started=" + started +
156+
", finished=" + finished +
157+
", reason='" + reason + '\'' +
158+
", result=" + result +
159+
", dynamic=" + dynamic +
160+
'}';
161+
}
149162
}

0 commit comments

Comments
 (0)