Skip to content
Permalink
Browse files
Merge pull request #34 from willemv/master
[FIXED JENKINS-12205] : show test statistics for Matrix Jobs
  • Loading branch information
willemv committed Sep 18, 2015
2 parents 09948c5 + dabdd8e commit 117b3666733afa3a6507f8d59ff6568adfdfd6a8
Showing 3 changed files with 115 additions and 28 deletions.
@@ -1,10 +1,10 @@
package hudson.plugins.view.dashboard.test;

import hudson.matrix.MatrixProject;
import hudson.maven.reporters.SurefireAggregatedReport;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TopLevelItem;
import hudson.tasks.junit.TestResultAction;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestResultProjectAction;

@@ -15,54 +15,67 @@ public class TestUtil {
/**
* Summarize the last test results from the passed set of jobs. If a job
* doesn't include any tests, add a 0 summary.
*
*
* @param jobs
* @return
*/
public static TestResultSummary getTestResultSummary(Collection<TopLevelItem> jobs) {
TestResultSummary summary = new TestResultSummary();

for (TopLevelItem item : jobs) {
if (item instanceof Job) {
Job job = (Job) item;
boolean addBlank = true;
TestResultProjectAction testResults = job.getAction(TestResultProjectAction.class);
for (TopLevelItem item : jobs) {
if (item instanceof MatrixProject) {
MatrixProject mp = (MatrixProject) item;

if (testResults != null) {
AbstractTestResultAction tra = testResults.getLastTestResultAction();
for (Job job : mp.getAllJobs()) {
if (job != mp) { //getAllJobs includes the parent job too, so skip that
summarizeJob(job, summary);
}
}
}
else if (item instanceof Job) {
Job job = (Job) item;
summarizeJob(job, summary);
}
}

return summary;
}

private static void summarizeJob(Job job, TestResultSummary summary) {
boolean addBlank = true;
TestResultProjectAction testResults = job.getAction(TestResultProjectAction.class);

if (tra != null) {
addBlank = false;
summary.addTestResult(new TestResult(job, tra.getTotalCount(), tra.getFailCount(), tra.getSkipCount()));
}
} else {
SurefireAggregatedReport surefireTestResults = job.getAction(SurefireAggregatedReport.class);
if (surefireTestResults != null) {
addBlank = false;
summary.addTestResult(new TestResult(job, surefireTestResults.getTotalCount(), surefireTestResults.getFailCount(), surefireTestResults.getSkipCount()));
}
}
if (testResults != null) {
AbstractTestResultAction tra = testResults.getLastTestResultAction();

if (addBlank) {
summary.addTestResult(new TestResult(job, 0, 0, 0));
}
}
if (tra != null) {
addBlank = false;
summary.addTestResult(new TestResult(job, tra.getTotalCount(), tra.getFailCount(), tra.getSkipCount()));
}
} else {
SurefireAggregatedReport surefireTestResults = job.getAction(SurefireAggregatedReport.class);
if (surefireTestResults != null) {
addBlank = false;
summary.addTestResult(new TestResult(job, surefireTestResults.getTotalCount(), surefireTestResults.getFailCount(), surefireTestResults.getSkipCount()));
}
}

return summary;
if (addBlank) {
summary.addTestResult(new TestResult(job, 0, 0, 0));
}
}

public static TestResult getTestResult(Run run) {
AbstractTestResultAction tra = run.getAction(AbstractTestResultAction.class);
if (tra != null) {
return new TestResult(run.getParent(), tra.getTotalCount(), tra.getFailCount(), tra.getSkipCount());
}
}

SurefireAggregatedReport surefireTestResults = run.getAction(SurefireAggregatedReport.class);
if (surefireTestResults != null) {
return new TestResult(run.getParent(), surefireTestResults.getTotalCount(), surefireTestResults.getFailCount(), surefireTestResults.getSkipCount());
}

return new TestResult(run.getParent(), 0, 0, 0);
}
}
@@ -0,0 +1,60 @@
package hudson.plugins.view.dashboard.test;

import hudson.Launcher;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.matrix.TextAxis;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.TopLevelItem;
import hudson.tasks.junit.JUnitResultArchiver;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestBuilder;

import java.io.IOException;
import java.util.Collections;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class TestSummaryForMatrixJobs {
@Rule
public JenkinsRule jenkins = new JenkinsRule();

private MatrixProject matrixProject;

@Before
public void createMatrixJob() throws IOException {
matrixProject = jenkins.createMatrixProject("top");
matrixProject.getAxes().add(new TextAxis("param", "one", "two"));
matrixProject.getBuildersList().add(new TestBuilder() {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
build.getWorkspace().child("testresult.xml").copyFrom(TestSummaryForMatrixJobs.class.getResource("/hudson/plugins/view/dashboard/test_failure.xml"));
return true;
}
});
matrixProject.getPublishersList().add(new JUnitResultArchiver("testresult.xml", true, null));
}

@Test
public void summaryIncludesMatrixJobs() throws Exception {
MatrixBuild result = matrixProject.scheduleBuild2(0).get();
jenkins.assertBuildStatus(Result.UNSTABLE, result);

TestResultSummary testSummary = TestUtil.getTestResultSummary(Collections.singleton((TopLevelItem) matrixProject));
assertThat(testSummary.getFailed(), is((2)));
assertThat(testSummary.getSuccess(), is((2)));
}

@After
public void cleanupMatrixJob() throws IOException, InterruptedException {
matrixProject.delete();
}

}
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="0" failures="1" hostname="anniesue" name="com.luciad.format.aixmcommon.view.lightspeed.TLspAIXMStylerAcceptanceTest" skipped="0" tests="2" time="12.829" timestamp="2015-09-14T14:37:11">
<properties/>
<testcase classname="test" name="failure" time="0.376">
<failure message="description" type="junit.framework.AssertionFailedError">
detail message
</failure>
</testcase>
<testcase classname="test.case" name="success" time="0.893" />
<system-out><![CDATA[System out
]]></system-out>
<system-err><![CDATA[System err
]]></system-err>
</testsuite>

0 comments on commit 117b366

Please sign in to comment.