Skip to content

Commit

Permalink
Add support for @afterall in XML report
Browse files Browse the repository at this point in the history
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>
```
  • Loading branch information
fzakaria committed Oct 10, 2024
1 parent 43d61cb commit 3f9fd44
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ private Map<TestData, List<TestData>> matchTestCasesToSuites_locked(
// In all cases we're looking for the "class" segment, pull the UniqueID and map that from the
// results
List<UniqueId.Segment> segments = testCase.getId().getUniqueIdObject().getSegments();

if (segments.size() == 3 || segments.size() == 5) {

if (segments.size() == 2) {
parent = results.get(testCase.getId().getUniqueIdObject());
}
else if (segments.size() == 3 || segments.size() == 5) {
// get class / test data up one level
parent =
testCase
Expand Down Expand Up @@ -161,7 +164,7 @@ private List<TestData> findTestCases_locked() {
// are identified by the fact that they have no child test cases in the
// test plan, or they are marked as tests.
TestIdentifier id = result.getId();
return id.isTest() || testPlan.getChildren(id).isEmpty();
return id.getSource() != null || id.isTest() || testPlan.getChildren(id).isEmpty();
})
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,17 @@ public boolean isDynamic() {
public Instant getStarted() {
return this.started;
}

@Override
public String toString() {
return "TestData{" +
"id=" + id +
", reportEntries=" + reportEntries +
", started=" + started +
", finished=" + finished +
", reason='" + reason + '\'' +
", result=" + result +
", dynamic=" + dynamic +
'}';
}
}

0 comments on commit 3f9fd44

Please sign in to comment.