Skip to content
Permalink
Browse files

Merge pull request #54 from kgyrtkirk/JENKINS-37598

[JENKINS-37598] Inaccurate aggregation of multiple xmls containing case information about the same testsuite
  • Loading branch information...
olivergondza committed Aug 11, 2017
2 parents 6243fa5 + 319280f commit d083744e64c6f1a084bc032f9ef8f8a9e45cf38e
@@ -33,7 +33,9 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Cumulative test result of a test class.
@@ -176,7 +178,7 @@ public void add(CaseResult r) {
@Override
public void tally() {
passCount=failCount=skipCount=0;
duration=0;
Set<SuiteResult> suites=new HashSet<SuiteResult>();
for (CaseResult r : cases) {
r.setClass(this);
if (r.isSkipped()) {
@@ -188,10 +190,13 @@ else if(r.isPassed()) {
else {
failCount++;
}
//retrieve the class duration from these cases' suite time
if(duration == 0){
duration = r.getSuiteResult().getDuration();
}
suites.add( r.getSuiteResult() );
}

// retrieve the class duration from these cases' suite time
duration = 0;
for (SuiteResult s : suites) {
duration += s.getDuration();
}
}

@@ -42,6 +42,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@@ -60,6 +61,7 @@
*/
@ExportedBean
public final class SuiteResult implements Serializable {
private static final Logger LOGGER = Logger.getLogger(SuiteResult.class.getName());
private final String file;
private final String name;
private final String stdout;
@@ -177,7 +179,7 @@ private SuiteResult(File xmlReport, Element suite, boolean keepLongStdio) throws
if( ( this.time = suite.attributeValue("time") ) != null ){
duration = new TimeToFloat(this.time).parse();
}

Element ex = suite.element("error");
if(ex!=null) {
// according to junit-noframes.xsl l.229, this happens when the test class failed to load
@@ -236,11 +238,18 @@ private SuiteResult(File xmlReport, Element suite, boolean keepLongStdio) throws
casesByName().put(cr.getName(), cr);

//if suite time was not specified use sum of the cases' times
if(this.time == null){
if( !hasTimeAttr() ){
duration += cr.getDuration();
}
}

/**
* Returns true if the time attribute is present in this Suite.
*/
private boolean hasTimeAttr() {
return time != null;
}

@Exported(visibility=9)
public String getName() {
return name;
@@ -357,4 +366,22 @@ void setParent(hudson.tasks.junit.TestResult parent) {
private static final long serialVersionUID = 1L;

private static final Pattern SUREFIRE_FILENAME = Pattern.compile("TEST-(.+)\\.xml");

/**
* Merges another SuiteResult into this one.
*
* @param sr the SuiteResult to merge into this one
*/
public void merge(SuiteResult sr) {
if (sr.hasTimeAttr() ^ hasTimeAttr()){
LOGGER.warning("Merging of suiteresults with incompatible time attribute may lead to incorrect durations in reports.( "+getFile()+", "+sr.getFile()+")");
}
if (hasTimeAttr()) {
duration += sr.getDuration();
}
for (CaseResult cr : sr.getCases()) {
addCase(cr);
cr.replaceParent(this);
}
}
}
@@ -256,12 +256,9 @@ private void add(SuiteResult sr) {
if(strictEq(s.getTimestamp(),sr.getTimestamp())) {
return;
}

for (CaseResult cr: sr.getCases()) {
s.addCase(cr);
cr.replaceParent(s);
}

duration += sr.getDuration();
s.merge(sr);
return;
}
}
@@ -171,6 +171,32 @@ public void testMerge() throws IOException, URISyntaxException {
assertEquals("Fail count should now be 1", 1, first.getFailCount());
}

@Issue("JENKINS-37598")
@Test
public void testMergeWithTime() throws Exception {
TestResult testResult = new TestResult();
testResult.parse(getDataFile("junit-report-time-aggregation.xml"));
testResult.tally();

assertEquals(1, testResult.getSuites().size());
SuiteResult suite = testResult.getSuite("test.fs.FileSystemTests");
assertEquals(3, suite.getCases().size());
assertEquals(100, suite.getDuration(), 2);
}

@Issue("JENKINS-37598")
@Test
public void testMergeWithoutTime() throws Exception {
TestResult testResult = new TestResult();
testResult.parse(getDataFile("junit-report-time-aggregation2.xml"));
testResult.tally();

assertEquals(1, testResult.getSuites().size());
SuiteResult suite = testResult.getSuite("test.fs.FileSystemTests");
assertEquals(3, suite.getCases().size());
assertEquals(30, suite.getDuration(), 2);
}

private static final XStream XSTREAM = new XStream2();

static {
@@ -179,4 +205,4 @@ public void testMerge() throws IOException, URISyntaxException {
XSTREAM.alias("case",CaseResult.class);
XSTREAM.registerConverter(new HeapSpaceStringConverter(),100);
}
}
}
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Automation Tests" tests="2" errors="0" failures="0" ignored="0">
<testsuite name="test.fs.FileSystemTests" time="50">
<testcase name="testPrefix1" classname="test.fs.FileSystemTest1" time="10"/>
<testcase name="testPrefix1A" classname="test.fs.FileSystemTest1" time="10"/>
</testsuite>
<testsuite name="test.fs.FileSystemTests" time="50">
<testcase name="testPrefix2" classname="test.fs.FileSystemTest1" time="10"/>
</testsuite>
</testsuites>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Automation Tests" tests="2" errors="0" failures="0" ignored="0">
<testsuite name="test.fs.FileSystemTests">
<testcase name="testPrefix1" classname="test.fs.FileSystemTest1" time="10"/>
<testcase name="testPrefix1A" classname="test.fs.FileSystemTest1" time="10"/>
</testsuite>
<testsuite name="test.fs.FileSystemTests">
<testcase name="testPrefix2" classname="test.fs.FileSystemTest1" time="10"/>
</testsuite>
</testsuites>

0 comments on commit d083744

Please sign in to comment.
You can’t perform that action at this time.