Skip to content
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

Programming exercises: Support more test result formats in LocalCI #9691

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -22,6 +22,9 @@ public class TestResultXmlParser {

/**
* Parses the test result file and extracts failed and successful tests.
* The name of nested testsuite elements are prepended with dots to the testcase name.
* A singular top-level testsuite is not included in the name.
* If multiple top-level testsuite elements are present, their names will be included.
*
* @param testResultFileString The content of the test result file as a String.
* @param failedTests A list of failed tests. This list will be populated by the method.
Expand All @@ -33,31 +36,63 @@ public static void processTestResultFile(String testResultFileString, List<Build
testResultFileString = testResultFileString.replaceAll(INVALID_XML_CHARS, "");
TestSuite testSuite = mapper.readValue(testResultFileString, TestSuite.class);

// A toplevel <testsuites> element is parsed like a <testsuite>
processTestSuite(testSuite, failedTests, successfulTests);
// The toplevel element can be <testsuites> or <testsuite>
if (testResultFileString.contains("<testsuites")) {
krusche marked this conversation as resolved.
Show resolved Hide resolved
if (testSuite.testSuites().size() == 1) {
TestSuite suite = testSuite.testSuites().getFirst();
processTopLevelTestSuite(failedTests, successfulTests, suite);
}
else {
for (TestSuite suite : testSuite.testSuites()) {
processInnerTestSuite(suite, failedTests, successfulTests, "");
}
}
}
else {
processTopLevelTestSuite(failedTests, successfulTests, testSuite);
}
b-fein marked this conversation as resolved.
Show resolved Hide resolved
}

private static void processTopLevelTestSuite(List<BuildResult.LocalCITestJobDTO> failedTests, List<BuildResult.LocalCITestJobDTO> successfulTests, TestSuite suite) {
processTestSuiteWithNamePrefix(suite, failedTests, successfulTests, "");
}

private static void processInnerTestSuite(TestSuite testSuite, List<BuildResult.LocalCITestJobDTO> failedTests, List<BuildResult.LocalCITestJobDTO> successfulTests,
String outerNamePrefix) {
String namePrefix;
if (testSuite.name() != null) {
namePrefix = outerNamePrefix + testSuite.name() + ".";
}
else {
namePrefix = outerNamePrefix;
}

processTestSuiteWithNamePrefix(testSuite, failedTests, successfulTests, namePrefix);
}

private static void processTestSuite(TestSuite testSuite, List<BuildResult.LocalCITestJobDTO> failedTests, List<BuildResult.LocalCITestJobDTO> successfulTests) {
private static void processTestSuiteWithNamePrefix(TestSuite testSuite, List<BuildResult.LocalCITestJobDTO> failedTests, List<BuildResult.LocalCITestJobDTO> successfulTests,
String namePrefix) {
for (TestCase testCase : testSuite.testCases()) {
if (testCase.isSkipped()) {
continue;
}
Failure failure = testCase.extractFailure();
if (failure != null) {
failedTests.add(new BuildResult.LocalCITestJobDTO(testCase.name(), List.of(failure.extractMessage())));
failedTests.add(new BuildResult.LocalCITestJobDTO(namePrefix + testCase.name(), List.of(failure.extractMessage())));
magaupp marked this conversation as resolved.
Show resolved Hide resolved
}
else {
successfulTests.add(new BuildResult.LocalCITestJobDTO(testCase.name(), List.of()));
successfulTests.add(new BuildResult.LocalCITestJobDTO(namePrefix + testCase.name(), List.of()));
}
}

for (TestSuite suite : testSuite.testSuites()) {
processTestSuite(suite, failedTests, successfulTests);
processInnerTestSuite(suite, failedTests, successfulTests, namePrefix);
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
record TestSuite(@JacksonXmlElementWrapper(useWrapping = false) @JacksonXmlProperty(localName = "testcase") List<TestCase> testCases,
record TestSuite(@JacksonXmlProperty(isAttribute = true, localName = "name") String name,
@JacksonXmlElementWrapper(useWrapping = false) @JacksonXmlProperty(localName = "testcase") List<TestCase> testCases,
@JacksonXmlElementWrapper(useWrapping = false) @JacksonXmlProperty(localName = "testsuite") List<TestSuite> testSuites) {

TestSuite {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,77 @@ void testNestedTestsuite() throws IOException {
""";

TestResultXmlParser.processTestResultFile(input, failedTests, successfulTests);
assertThat(successfulTests).hasSize(12).extracting(BuildResult.LocalCITestJobDTO::getName).containsExactlyInAnyOrder("Testing filtering in A", "Testing mapping in A",
"Testing filtering in B", "Testing mapping in B", "Testing filtering in C", "Testing mapping in C", "Testing A against sample solution",
"Testing B against sample solution", "Testing C against sample solution", "Testing selectAndReflectA (0,0) []", "Testing selectAndReflectB (0,1) [(0,0)]",
"Testing selectAndReflectC (0,1) [(-1,-1)]");

// @formatter:off
assertThat(successfulTests).extracting(BuildResult.LocalCITestJobDTO::getName).containsExactlyInAnyOrder(
"Properties.Checked by SmallCheck.Testing filtering in A",
"Properties.Checked by SmallCheck.Testing mapping in A",
"Properties.Checked by SmallCheck.Testing filtering in B",
"Properties.Checked by SmallCheck.Testing mapping in B",
"Properties.Checked by SmallCheck.Testing filtering in C",
"Properties.Checked by SmallCheck.Testing mapping in C",
"Properties.Checked by QuickCheck.Testing A against sample solution",
"Properties.Checked by QuickCheck.Testing B against sample solution",
"Properties.Checked by QuickCheck.Testing C against sample solution",
"Unit Tests.Testing selectAndReflectA (0,0) []",
"Unit Tests.Testing selectAndReflectB (0,1) [(0,0)]",
"Unit Tests.Testing selectAndReflectC (0,1) [(-1,-1)]");
// @formatter:on
assertThat(failedTests).isEmpty();
}

@Test
void testMultipleTestsuite() throws IOException {
magaupp marked this conversation as resolved.
Show resolved Hide resolved
String input = """
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="SuiteA" tests="3">
<testcase name="Test1"/>
<testcase name="Test2"/>
<testcase name="Test3"/>
</testsuite>
<testsuite name="SuiteB" tests="3">
<testcase name="Test1"/>
<testcase name="Test2"/>
<testcase name="Test3"/>
</testsuite>
</testsuites>
""";

TestResultXmlParser.processTestResultFile(input, failedTests, successfulTests);

// @formatter:off
assertThat(successfulTests).extracting(BuildResult.LocalCITestJobDTO::getName).containsExactlyInAnyOrder(
"SuiteA.Test1",
"SuiteA.Test2",
"SuiteA.Test3",
"SuiteB.Test1",
"SuiteB.Test2",
"SuiteB.Test3");
// @formatter:on
assertThat(failedTests).isEmpty();
}

@Test
void testNestedTestsuiteMissingNames() throws IOException {
String input = """
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite>
<testsuite>
<testsuite>
<testcase name="Test1"/>
<testcase name="Test2"/>
<testcase name="Test3"/>
</testsuite>
</testsuite>
</testsuite>
</testsuites>
""";

TestResultXmlParser.processTestResultFile(input, failedTests, successfulTests);

assertThat(successfulTests).extracting(BuildResult.LocalCITestJobDTO::getName).containsExactlyInAnyOrder("Test1", "Test2", "Test3");
assertThat(failedTests).isEmpty();
}
}
Loading