Skip to content

resolve issue 319, handling test method name for Parameterized tests #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,7 @@ public void toXml(XMLStreamWriter xml, TestData test) throws XMLStreamException

TestIdentifier id = test.getId();

String name;
if (test.isDynamic()) {
name = id.getDisplayName(); // [ordinal] name=value...
} else {
// Massage the name
name = id.getLegacyReportingName();
int index = name.indexOf('(');
if (index != -1) {
name = name.substring(0, index);
}
}
String name = getTestName(test);

xml.writeStartElement("testcase");
xml.writeAttribute("name", escapeIllegalCharacters(name));
Expand Down Expand Up @@ -104,4 +94,32 @@ private void writeThrowableMessage(XMLStreamWriter xml, TestExecutionResult resu

xml.writeCData(escapeIllegalCharacters(stringWriter.toString()));
}

private String getTestName(TestData test) {
TestIdentifier id = test.getId();
// checking for the '[' as a proxy for the ordinal parameterized test.
// If there is some edge case not considered, here, it should still be okay, as it really just
// formats the test case name.
if (id.getParentId().isPresent() && id.getDisplayName().startsWith("[")) {
return getCustomDisplayName(id);
}
// this check assumes ParameterizedTest is dynamic, leaving this for now but may not need it.
// Edge case maybe when there is no parent (if ever possible)
if (test.isDynamic()) {
return id.getDisplayName(); // [ordinal] name=value...
} else {
String name = id.getLegacyReportingName();
int index = name.indexOf('(');
if (index != -1) {
name = name.substring(0, index);
}
return name;
}
}

private String getCustomDisplayName(TestIdentifier testIdentifier) {
TestIdentifier parent = testPlan.getTestIdentifier(testIdentifier.getParentId().get());
String methodName = parent.getDisplayName().replaceAll("\\(\\)", "");
return methodName + " " + testIdentifier.getDisplayName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@
import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;

class TestSuiteXmlRenderer {

private final TestCaseXmlRenderer testRenderer;
private final TestPlan testPlan;

public TestSuiteXmlRenderer(TestPlan testPlan) {
testRenderer = new TestCaseXmlRenderer(testPlan);
this.testPlan = testPlan;
}

public void toXml(XMLStreamWriter xml, TestData suite, Collection<TestData> tests)
Expand Down Expand Up @@ -73,8 +82,38 @@ public void toXml(XMLStreamWriter xml, TestData suite, Collection<TestData> test
xml.writeAttribute("package", "");
xml.writeEmptyElement("properties");

// Builds a map of testMethods and matching test cases to wrap in a test suite, which is used to
// group parameterized tests
// Ensures grouping for parameterized tests.
Map<String, List<TestData>> testMethods = new HashMap<>();

for (TestData testCase : tests) {
testRenderer.toXml(xml, testCase);
Optional<TestIdentifier> parentOptional =
testCase.getId().getParentId().map(testPlan::getTestIdentifier);

if (parentOptional.isPresent()) {
TestIdentifier parent = parentOptional.get();
String methodName = parent.getDisplayName().replaceAll("\\(\\)", "");
testMethods.computeIfAbsent(methodName, k -> new ArrayList<>()).add(testCase);
} else {
testRenderer.toXml(xml, testCase);
}
}

// Wrapping each group of related tests by method name, and wraps in a testsuite tag, and then
// build a testcase within the suite for each testCase
for (String methodName : testMethods.keySet()) {
xml.writeStartElement("testsuite");
xml.writeAttribute("name", methodName);
// Lexicographphically sorted should be suffcient since [1] < [2] < [3] etc
List<TestData> sortedTestCases = testMethods.get(methodName);
Collections.sort(
sortedTestCases,
(a, b) -> a.getId().getDisplayName().compareTo(b.getId().getDisplayName()));
for (TestData testCase : testMethods.get(methodName)) {
testRenderer.toXml(xml, testCase);
}
xml.writeEndElement();
}

writeTextElement(xml, "system-out", suite.getStdOut());
Expand Down
Loading