Skip to content

Commit 6b15ddf

Browse files
committed
o Breaking API changes for TestReport in Jenkins 2 which is caused by different REST API answers for Maven Job type and other job types.
1 parent 74eac36 commit 6b15ddf

4 files changed

Lines changed: 127 additions & 1 deletion

File tree

ReleaseNotes.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,85 @@
1616

1717
### API Changes
1818

19+
* [Fixed issue 184][issue-184]
20+
21+
Based on the differences between getting a TestReport for a MavenJob type and
22+
a freestyle job etc. you would have hit by getting 0 from `getTotalCount()` like
23+
in the following code snippet:
24+
25+
```java
26+
JobWithDetails job = js.getJob("non-maven-test");
27+
Build lastCompletedBuild = job.getLastCompletedBuild();
28+
TestReport testReport = lastCompletedBuild.getTestReport();
29+
```
30+
31+
This is caused by the difference in the API of Jenkins which results in the following
32+
for a MavenJob type:
33+
34+
{
35+
"_class" : "hudson.maven.reporters.SurefireAggregatedReport",
36+
"failCount" : 0,
37+
"skipCount" : 0,
38+
"totalCount" : 489,
39+
"urlName" : "testReport",
40+
"childReports" : [
41+
{
42+
"child" : {
43+
"_class" : "hudson.maven.MavenBuild",
44+
"number" : 2,
45+
"url" : "http://localhost:27100/buildserver/job/maven-test/com.soebes.subversion.sapm$sapm/2/"
46+
},
47+
"result" : {
48+
"_class" : "hudson.tasks.junit.TestResult",
49+
"testActions" : [
50+
51+
],
52+
"duration" : 0.009,
53+
"empty" : false,
54+
"failCount" : 0,
55+
"passCount" : 489,
56+
"skipCount" : 0,
57+
"suites" : [
58+
{
59+
"cases" : [
60+
61+
But for a non Maven job like freestyle job you will get the following:
62+
63+
{
64+
"_class" : "hudson.tasks.junit.TestResult",
65+
"testActions" : [
66+
67+
],
68+
"duration" : 0.01,
69+
"empty" : false,
70+
"failCount" : 0,
71+
"passCount" : 489,
72+
"skipCount" : 0,
73+
"suites" : [
74+
{
75+
"cases" : [
76+
{
77+
"testActions" : [
78+
79+
],
80+
"age" : 0,
81+
"className" : "com.soebes.subversion.sapm.AccessRuleGroupTest",
82+
"duration" : 0.003,
83+
84+
This is exactly the cause for this result.
85+
86+
The API has been enhanced to get the correct result. This can be achieved by calling
87+
the following in cases where you have a non Maven job type.
88+
89+
```java
90+
TestResult testResult = lastCompletedBuild.getTestResult();
91+
```
92+
93+
This means you need to take care yourself if you are getting the test results from
94+
Maven job type or non Maven job. (Future releases of the lib hopefully handle that
95+
in a more convenient way).
96+
97+
1998

2099
* [Fixed issue 209][issue-209]
21100

@@ -825,6 +904,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport();
825904
[issue-176]: https://github.com/jenkinsci/java-client-api/issues/176
826905
[issue-179]: https://github.com/jenkinsci/java-client-api/issues/179
827906
[issue-182]: https://github.com/jenkinsci/java-client-api/issues/182
907+
[issue-184]: https://github.com/jenkinsci/java-client-api/issues/184
828908
[issue-186]: https://github.com/jenkinsci/java-client-api/issues/186
829909
[issue-188]: https://github.com/jenkinsci/java-client-api/issues/188
830910
[issue-197]: https://github.com/jenkinsci/java-client-api/issues/197

jenkins-client/src/main/java/com/offbytwo/jenkins/model/Build.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public TestReport getTestReport() throws IOException {
109109
return client.get(this.getUrl() + "/testReport/?depth=1", TestReport.class);
110110
}
111111

112+
//Only for non Maven Jobs..
113+
public TestResult getTestResult() throws IOException {
114+
return client.get(this.getUrl() + "/testReport/?depth=1", TestResult.class);
115+
}
116+
112117
/*
113118
* This Change (Bad Practice) is due to inconsistencies in Jenkins various
114119
* versions. Jenkins changed their API from post to get and from get to post

jenkins-client/src/main/java/com/offbytwo/jenkins/model/TestResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @author Karl Heinz Marbaise
77
*
88
*/
9-
public class TestResult {
9+
public class TestResult extends BaseModel {
1010

1111
private double duration;
1212
private boolean empty;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.offbytwo.jenkins;
2+
3+
import java.net.URI;
4+
import java.util.List;
5+
6+
import org.junit.Test;
7+
8+
import com.offbytwo.jenkins.model.Build;
9+
import com.offbytwo.jenkins.model.JobWithDetails;
10+
import com.offbytwo.jenkins.model.TestReport;
11+
import com.offbytwo.jenkins.model.TestResult;
12+
import com.offbytwo.jenkins.model.TestSuites;
13+
14+
public class JenkinsTestManualTestReport {
15+
16+
@Test
17+
public void firstTest() throws Exception {
18+
19+
JenkinsServer js = new JenkinsServer(URI.create("http://localhost:10090/buildserver/"), "admin", "admin");
20+
JobWithDetails job = js.getJob("non-maven-test");
21+
Build lastCompletedBuild = job.getLastCompletedBuild();
22+
TestReport testReport = lastCompletedBuild.getTestReport();
23+
24+
System.out.println(" --- TestReport ---");
25+
System.out.println("totalCount: " + testReport.getTotalCount());
26+
System.out.println(" failCount: " + testReport.getFailCount());
27+
System.out.println(" skipCount: " + testReport.getSkipCount());
28+
29+
TestResult testResult = lastCompletedBuild.getTestResult();
30+
System.out.println(" --- TestResult ---");
31+
32+
System.out.println(" PassCount: " + testResult.getPassCount());
33+
System.out.println(" failCount: " + testResult.getFailCount());
34+
System.out.println(" skipCount: " + testResult.getSkipCount());
35+
System.out.println(" duration: " + testResult.getDuration());
36+
System.out.println(" isEmpty: " + testResult.isEmpty());
37+
List<TestSuites> suites = testResult.getSuites();
38+
39+
40+
}
41+
}

0 commit comments

Comments
 (0)