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

Added option to fail the build if no tests were run #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,16 @@ public class ExtraConfiguration implements Serializable {

private final long testTimeMargin;

public ExtraConfiguration(long testTimeMargin) {
private final boolean failIfNoTestsRun;

public ExtraConfiguration(long testTimeMargin, boolean failIfNoTestsRun) {
this.testTimeMargin = testTimeMargin;
this.failIfNoTestsRun = failIfNoTestsRun;
}

public long getTestTimeMargin() {
return testTimeMargin;
}

public boolean getFailIfNoTestsRun() { return failIfNoTestsRun; }
}
15 changes: 12 additions & 3 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,16 @@ public XUnitBuilder(TestType[] types, XUnitThreshold[] thresholds) {
}

@DataBoundConstructor
public XUnitBuilder(TestType[] tools, XUnitThreshold[] thresholds, int thresholdMode, String testTimeMargin) {
public XUnitBuilder(TestType[] tools, XUnitThreshold[] thresholds, int thresholdMode, String testTimeMargin,
boolean failIfNoTestsRun) {
this.types = tools;
this.thresholds = thresholds;
this.thresholdMode = thresholdMode;
long longTestTimeMargin = XUnitDefaultValues.TEST_REPORT_TIME_MARGING;
if (testTimeMargin != null && testTimeMargin.trim().length() != 0) {
longTestTimeMargin = Long.parseLong(testTimeMargin);
}
this.extraConfiguration = new ExtraConfiguration(longTestTimeMargin);
this.extraConfiguration = new ExtraConfiguration(longTestTimeMargin, failIfNoTestsRun);
}

/**
Expand All @@ -94,6 +95,13 @@ public String getTestTimeMargin() {
return String.valueOf(getExtraConfiguration().getTestTimeMargin());
}

/**
* Needed to support Snippet Generator and Workflow properly
*/
public boolean getFailIfNoTestsRun() {
return getExtraConfiguration().getFailIfNoTestsRun();
}

public TestType[] getTypes() {
return types;
}
Expand All @@ -108,7 +116,8 @@ public int getThresholdMode() {

public ExtraConfiguration getExtraConfiguration() {
if (extraConfiguration == null) {
extraConfiguration = new ExtraConfiguration(XUnitDefaultValues.TEST_REPORT_TIME_MARGING);
extraConfiguration = new ExtraConfiguration(XUnitDefaultValues.TEST_REPORT_TIME_MARGING,
XUnitDefaultValues.FAIL_IF_NO_TEST_RUN);
}
return extraConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ public class XUnitDefaultValues {
public static final int MODE_PERCENT = 2;

public static final int TEST_REPORT_TIME_MARGING = 3000; //default to 3000ms

public static final boolean FAIL_IF_NO_TEST_RUN = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,14 @@ private void recordTestResult(Run<?, ?> build, FilePath workspace, TaskListener
}

if (result.getPassCount() == 0 && result.getFailCount() == 0) {
xUnitLog.warningConsoleLogger("All test reports are empty.");
String message = "All test reports are empty.";
if(extraConfiguration.getFailIfNoTestsRun()){
xUnitLog.errorConsoleLogger(message);
build.setResult(Result.FAILURE);
} else {
xUnitLog.warningConsoleLogger(message);
}

}

if (existingAction == null) {
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/jenkinsci/plugins/xunit/XUnitPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ public XUnitPublisher(TestType[] types, XUnitThreshold[] thresholds) {
}

@DataBoundConstructor
public XUnitPublisher(TestType[] tools, XUnitThreshold[] thresholds, int thresholdMode, String testTimeMargin) {
public XUnitPublisher(TestType[] tools, XUnitThreshold[] thresholds, int thresholdMode, String testTimeMargin,
boolean failIfNoTestsRun) {
this.types = tools;
this.thresholds = thresholds;
this.thresholdMode = thresholdMode;
long longTestTimeMargin = XUnitDefaultValues.TEST_REPORT_TIME_MARGING;
if (testTimeMargin != null && testTimeMargin.trim().length() != 0) {
longTestTimeMargin = Long.parseLong(testTimeMargin);
}
this.extraConfiguration = new ExtraConfiguration(longTestTimeMargin);
this.extraConfiguration = new ExtraConfiguration(longTestTimeMargin, failIfNoTestsRun);
}

/**
Expand All @@ -99,6 +100,14 @@ public String getTestTimeMargin() {
return String.valueOf(getExtraConfiguration().getTestTimeMargin());
}

/**
* Needed to support Snippet Generator and Workflow properly
*/
public boolean getFailIfNoTestsRun() {
return getExtraConfiguration().getFailIfNoTestsRun();
}


public TestType[] getTypes() {
return types;
}
Expand All @@ -113,7 +122,8 @@ public int getThresholdMode() {

public ExtraConfiguration getExtraConfiguration() {
if (extraConfiguration == null) {
extraConfiguration = new ExtraConfiguration(XUnitDefaultValues.TEST_REPORT_TIME_MARGING);
extraConfiguration = new ExtraConfiguration(XUnitDefaultValues.TEST_REPORT_TIME_MARGING,
XUnitDefaultValues.FAIL_IF_NO_TEST_RUN);
}
return extraConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,5 @@ public FilePath getUserContentRoot() {
public long getTestTimeMargin() {
return testTimeMargin;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ THE SOFTWARE.
</td>
<td width="50%" style="${td}"/>
</tr>
<tr>
<td width="50%" style="${td}">
<f:checkbox name="failIfNoTestsRun" checked="${instance.extraConfiguration.failIfNoTestsRun}" value="${instance.extraConfiguration.failIfNoTestsRun}"/>
<label>Fail the build if not a single test was run.
</label>
</td>
<td width="50%" style="${td}"/>
</tr>
</table>
</f:entry>
</f:advanced>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ THE SOFTWARE.
</td>
<td width="50%" style="${td}"/>
</tr>
<tr>
<td width="50%" style="${td}">
<f:checkbox name="failIfNoTestsRun" checked="${instance.extraConfiguration.failIfNoTestsRun}" value="${instance.extraConfiguration.failIfNoTestsRun}"/>
<label>Fail the build if not a single test was run.
</label>
</td>
<td width="50%" style="${td}"/>
</tr>
</table>
</f:entry>
</f:advanced>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ public class XUnitWorkflowTest {
@ClassRule
public static JenkinsRule jenkinsRule = new JenkinsRule();

private WorkflowJob getBaseJob(String jobName) throws Exception {
private WorkflowJob getBaseJob(String jobName, String xmlInput) throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, jobName);
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
FilePath input = workspace.child("input.xml");
input.copyFrom(XUnitWorkflowTest.class.getResourceAsStream("/org/jenkinsci/plugins/xunit/types/googletest/testcase2/input.xml"));
input.copyFrom(XUnitWorkflowTest.class.getResourceAsStream(xmlInput));

return job;
}

@Test
public void xunitBuilderWorkflowStepTest() throws Exception {
WorkflowJob job = getBaseJob("builder");
WorkflowJob job = getBaseJob("builder", "/org/jenkinsci/plugins/xunit/types/googletest/testcase2/input.xml");
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'XUnitBuilder', testTimeMargin: '3000', thresholdMode: 1, thresholds: [[$class: 'FailedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '1'], [$class: 'SkippedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '']], tools: [[$class: 'GoogleTestType', deleteOutputFiles: false, failIfNotNew: false, pattern: 'input.xml', skipNoTestFiles: false, stopProcessingIfError: true]]])\n"
Expand All @@ -58,12 +58,35 @@ public void xunitBuilderWorkflowStepTest() throws Exception {

@Test
public void xunitPublisherWorkflowStepTest() throws Exception {
WorkflowJob job = getBaseJob("publisher");
WorkflowJob job = getBaseJob("publisher", "/org/jenkinsci/plugins/xunit/types/googletest/testcase2/input.xml");
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'XUnitPublisher', testTimeMargin: '3000', thresholdMode: 1, thresholds: [[$class: 'FailedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '1'], [$class: 'SkippedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '']], tools: [[$class: 'GoogleTestType', deleteOutputFiles: false, failIfNotNew: false, pattern: 'input.xml', skipNoTestFiles: false, stopProcessingIfError: true]]])\n"
+ "}"));

jenkinsRule.assertBuildStatus(Result.UNSTABLE, job.scheduleBuild2(0).get());
}

@Test
public void xunitBuilderWorkflowStepTestFailIfNoTestRunDisabled() throws Exception {
WorkflowJob job = getBaseJob("builder-fail-when-no-test-run-disabled", "/org/jenkinsci/plugins/xunit/types/googletest/testcase3/input.xml");
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'XUnitBuilder', testTimeMargin: '3000', failIfNoTestsRun: false, thresholdMode: 1, thresholds: [[$class: 'FailedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '1'], [$class: 'SkippedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '']], tools: [[$class: 'GoogleTestType', deleteOutputFiles: false, failIfNotNew: false, pattern: 'input.xml', skipNoTestFiles: false, stopProcessingIfError: true]]])\n"
+ "}"));

jenkinsRule.assertBuildStatus(Result.SUCCESS, job.scheduleBuild2(0).get());
}

@Test
public void xunitBuilderWorkflowStepTestFailIfNoTestRunEnabled() throws Exception {
WorkflowJob job = getBaseJob("builder-fail-when-no-test-run-enabled", "/org/jenkinsci/plugins/xunit/types/googletest/testcase3/input.xml");
job.setDefinition(new CpsFlowDefinition(""
+ "node {\n"
+ " step([$class: 'XUnitBuilder', testTimeMargin: '3000', failIfNoTestsRun: true, thresholdMode: 1, thresholds: [[$class: 'FailedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '1'], [$class: 'SkippedThreshold', failureNewThreshold: '', failureThreshold: '', unstableNewThreshold: '', unstableThreshold: '']], tools: [[$class: 'GoogleTestType', deleteOutputFiles: false, failIfNotNew: false, pattern: 'input.xml', skipNoTestFiles: false, stopProcessingIfError: true]]])\n"
+ "}"));

jenkinsRule.assertBuildStatus(Result.FAILURE, job.scheduleBuild2(0).get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="0" failures="0" disabled="0" errors="0" time="0.042" name="AllTests">
</testsuites>