Skip to content

Commit

Permalink
add support for testsuites root element (fixes #532, via #534)
Browse files Browse the repository at this point in the history
  • Loading branch information
baev authored Aug 7, 2017
1 parent 264ebf5 commit 3575838
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
3 changes: 2 additions & 1 deletion plugins/junit-xml-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ dependencies {
testCompile('junit:junit')
testCompile('org.assertj:assertj-core')
testCompile('org.mockito:mockito-core')
testCompile("org.slf4j:slf4j-simple")
testCompile('org.slf4j:slf4j-simple')
testCompile('io.qameta.allure:allure-java-commons')
}

configurations.archives.artifacts.removeAll { it.archiveTask.is jar }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class JunitXmlPlugin implements Reader {
private static final BigDecimal MULTIPLICAND = new BigDecimal(1000);

private static final String TEST_SUITE_ELEMENT_NAME = "testsuite";
private static final String TEST_SUITES_ELEMENT_NAME = "testsuites";
private static final String TEST_CASE_ELEMENT_NAME = "testcase";
private static final String CLASS_NAME_ATTRIBUTE_NAME = "classname";
private static final String NAME_ATTRIBUTE_NAME = "name";
Expand All @@ -77,25 +78,32 @@ public class JunitXmlPlugin implements Reader {
@Override
public void readResults(final Configuration configuration, final ResultsVisitor visitor, final Path directory) {
final RandomUidContext context = configuration.requireContext(RandomUidContext.class);
listResults(directory).forEach(result -> parseTestSuite(directory, result, context, visitor));
listResults(directory).forEach(result -> parseRootElement(directory, result, context, visitor));
}

private void parseTestSuite(final Path resultsDirectory, final Path parsedFile,
final RandomUidContext context, final ResultsVisitor visitor) {
private void parseRootElement(final Path resultsDirectory, final Path parsedFile,
final RandomUidContext context, final ResultsVisitor visitor) {
try {
LOGGER.debug("Parsing file {}", parsedFile);
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();

final XmlElement testSuiteElement = new XmlElement(builder.parse(parsedFile.toFile()).getDocumentElement());
final String elementName = testSuiteElement.getName();
if (!TEST_SUITE_ELEMENT_NAME.equals(elementName)) {
LOGGER.debug("File {} is not a valid JUnit xml. Unknown root element {}", parsedFile, elementName);
final XmlElement rootElement = new XmlElement(builder.parse(parsedFile.toFile()).getDocumentElement());
final String elementName = rootElement.getName();

if (TEST_SUITE_ELEMENT_NAME.equals(elementName)) {
rootElement.get(TEST_CASE_ELEMENT_NAME)
.forEach(element -> parseTestCase(element, resultsDirectory, parsedFile, context, visitor));
return;
}

testSuiteElement.get(TEST_CASE_ELEMENT_NAME)
.forEach(element -> parseTestCase(element, resultsDirectory, parsedFile, context, visitor));
if (TEST_SUITES_ELEMENT_NAME.equals(elementName)) {
rootElement.get(TEST_SUITE_ELEMENT_NAME).stream()
.map(testSuiteElement -> testSuiteElement.get(TEST_CASE_ELEMENT_NAME))
.flatMap(Collection::stream)
.forEach(element -> parseTestCase(element, resultsDirectory, parsedFile, context, visitor));
return;
}
LOGGER.debug("File {} is not a valid JUnit xml. Unknown root element {}", parsedFile, elementName);
} catch (SAXException | ParserConfigurationException | IOException e) {
LOGGER.error("Could not parse file {}: {}", parsedFile, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.qameta.allure.junitxml;

import io.qameta.allure.Issue;
import io.qameta.allure.context.RandomUidContext;
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.ResultsVisitor;
Expand Down Expand Up @@ -198,6 +199,25 @@ public void shouldReadCdataMessage() throws Exception {

}

@Issue("532")
@Test
public void shouldParseSuitesTag() throws Exception {
process(
"junitdata/testsuites.xml", "TEST-test.SampleTest.xml"
);

final ArgumentCaptor<TestResult> captor = ArgumentCaptor.forClass(TestResult.class);
verify(visitor, times(3)).visitTestResult(captor.capture());

assertThat(captor.getAllValues())
.extracting(TestResult::getName)
.containsExactlyInAnyOrder(
"should default path to an empty string",
"should default consolidate to true",
"should default useDotNotation to true"
);
}

private void process(String... strings) throws IOException {
Path resultsDirectory = folder.newFolder().toPath();
Iterator<String> iterator = Arrays.asList(strings).iterator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="JUnitXmlReporter" errors="0" tests="0" failures="0" time="0" timestamp="2013-05-24T10:23:58" />
<testsuite name="JUnitXmlReporter.constructor" errors="0" skipped="1" tests="3" failures="1" time="0.006" timestamp="2013-05-24T10:23:58">
<properties>
<property name="java.vendor" value="Sun Microsystems Inc." />
<property name="compiler.debug" value="on" />
<property name="project.jdk.classpath" value="jdk.classpath.1.6" />
</properties>
<testcase classname="JUnitXmlReporter.constructor" name="should default path to an empty string" time="0.006">
<failure message="test failure">Assertion failed</failure>
</testcase>
<testcase classname="JUnitXmlReporter.constructor" name="should default consolidate to true" time="0">
<skipped />
</testcase>
<testcase classname="JUnitXmlReporter.constructor" name="should default useDotNotation to true" time="0" />
</testsuite>
</testsuites>

0 comments on commit 3575838

Please sign in to comment.