Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Use original JUnit test class name instead of suite class in test XML report #2624

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/com/facebook/buck/jvm/java/JavaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import java.util.SortedSet;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -422,10 +423,11 @@ public Path getPathToTestOutputDirectory() {
/** @return a test case result, named "main", signifying a failure of the entire test class. */
private TestCaseSummary getTestClassFailedSummary(String testClass, String message, long time) {
return new TestCaseSummary(
testClass,
ImmutableList.of(
new TestResultSummary(
testClass, "main", ResultType.FAILURE, time, message, "", "", "")));
testClass,
false,
ImmutableList.of(
new TestResultSummary(
testClass, "main", ResultType.FAILURE, time, message, "", "", "")));
}

@Override
Expand Down Expand Up @@ -473,7 +475,12 @@ public Callable<TestResults> interpretTestResults(
// definitively say whether or not a class should be run. It's not possible, for example,
// to filter testClassNames here at the buck end.
} else if (Files.isRegularFile(testResultFile)) {
summaries.add(XmlTestResultParser.parse(testResultFile));
TestCaseSummary summary = XmlTestResultParser.parse(testResultFile);
if (summary.isTestSuite()) {
summaries.addAll(unrollTestSuiteSummary(summary));
} else {
summaries.add(summary);
}
}
}

Expand Down Expand Up @@ -685,6 +692,15 @@ protected ImmutableSet<Path> getExtraRequiredPaths(
.build();
}

private List<TestCaseSummary> unrollTestSuiteSummary(TestCaseSummary summary) {
Map<String, List<TestResultSummary>> summariesByActualTestCase = summary.getTestResults()
.stream().collect(Collectors.groupingBy(TestResultSummary::getTestCaseName));

return summariesByActualTestCase.entrySet().stream()
.map(entry -> new TestCaseSummary(entry.getKey(), false, entry.getValue()))
.collect(Collectors.toList());
}

public interface AdditionalClasspathEntriesProvider {
ImmutableList<Path> getAdditionalClasspathEntries(SourcePathResolverAdapter resolver);
}
Expand Down
14 changes: 14 additions & 0 deletions src/com/facebook/buck/test/TestCaseSummary.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class TestCaseSummary implements TestCaseSummaryExternalInterface<TestRes
private static final int MAX_STATUS_WIDTH = 7;

private final String testCaseName;
private final boolean testSuite;
private final ImmutableList<TestResultSummary> testResults;
private final boolean isDryRun;
private final boolean hasAssumptionViolations;
Expand All @@ -38,7 +39,15 @@ public class TestCaseSummary implements TestCaseSummaryExternalInterface<TestRes
private final long totalTime;

public TestCaseSummary(String testCaseName, List<TestResultSummary> testResults) {
this(testCaseName, false, testResults);
}

public TestCaseSummary(
String testCaseName,
boolean testSuite,
List<TestResultSummary> testResults) {
this.testCaseName = testCaseName;
this.testSuite = testSuite;
this.testResults = ImmutableList.copyOf(testResults);

boolean isDryRun = false;
Expand Down Expand Up @@ -110,6 +119,11 @@ public long getTotalTime() {
return totalTime;
}

/** @return whether this summary represents a suite of tests or a single class */
public boolean isTestSuite() {
return testSuite;
}

/** @return a one-line, printable summary */
public String getOneLineSummary(Locale locale, boolean hasPassingDependencies, Ansi ansi) {
String statusText;
Expand Down
12 changes: 10 additions & 2 deletions src/com/facebook/buck/test/XmlTestResultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private static TestCaseSummary doParse(String xml) throws IOException, SAXExcept
Element root = doc.getDocumentElement();
Preconditions.checkState("testcase".equals(root.getTagName()));
String testCaseName = root.getAttribute("name");
boolean testSuite = false;

NodeList testElements = doc.getElementsByTagName("test");
List<TestResultSummary> testResults = Lists.newArrayListWithCapacity(testElements.getLength());
Expand Down Expand Up @@ -176,13 +177,20 @@ private static TestCaseSummary doParse(String xml) throws IOException, SAXExcept
stdErr = null;
}

String actualTestCaseName = node.getAttribute("suite");
if (actualTestCaseName != null && !actualTestCaseName.isEmpty()) {
testSuite |= !actualTestCaseName.equals(testCaseName);
} else {
actualTestCaseName = testCaseName;
}

TestResultSummary testResult =
new TestResultSummary(
testCaseName, testName, type, time, message, stacktrace, stdOut, stdErr);
actualTestCaseName, testName, type, time, message, stacktrace, stdOut, stdErr);
testResults.add(testResult);
}

return new TestCaseSummary(testCaseName, testResults);
return new TestCaseSummary(testCaseName, testSuite, testResults);
}

private static String createDetailedExceptionMessage(Path xmlFile, String xmlFileContents) {
Expand Down