Skip to content

Commit

Permalink
Fix threshold mode bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisEverling committed Jul 22, 2015
1 parent 14216bc commit b483f6e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ target/*
.settings/
.classpath
.project
/target/
47 changes: 25 additions & 22 deletions src/main/java/hudson/plugins/testng/Publisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ public class Publisher extends Recorder {
public final int failedSkips;
//number of fails that will trigger "Failed"
public final int failedFails;
public final boolean usePercentage;
public final int thresholdMode;


@Extension
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl();

@DataBoundConstructor
public Publisher(String reportFilenamePattern, boolean escapeTestDescp, boolean escapeExceptionMsg,
boolean showFailedBuilds, boolean failureOnFailedTestConfig, int unstableSkips, int unstableFails, int failedSkips, int failedFails, boolean usePercentage) {
public Publisher(String reportFilenamePattern, boolean escapeTestDescp, boolean escapeExceptionMsg, boolean showFailedBuilds, boolean failureOnFailedTestConfig, int unstableSkips, int unstableFails, int failedSkips, int failedFails, int thresholdMode) {
this.reportFilenamePattern = reportFilenamePattern;
this.escapeTestDescp = escapeTestDescp;
this.escapeExceptionMsg = escapeExceptionMsg;
Expand All @@ -67,7 +66,7 @@ public Publisher(String reportFilenamePattern, boolean escapeTestDescp, boolean
this.unstableFails = unstableFails;
this.failedSkips = failedSkips;
this.failedFails = failedFails;
this.usePercentage = usePercentage;
this.thresholdMode = thresholdMode;
}

public BuildStepMonitor getRequiredMonitorService() {
Expand Down Expand Up @@ -149,25 +148,29 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, final Build
if (failureOnFailedTestConfig && results.getFailedConfigCount() > 0) {
logger.println("Failed configuration methods found. Marking build as FAILURE.");
build.setResult(Result.FAILURE);
}
if(usePercentage) {
float failedPercent = 100 * results.getFailCount() / (float) results.getTotalCount();
float skipPercent = 100 * results.getSkipCount() / (float) results.getTotalCount();
if (failedPercent >= failedFails || skipPercent >= failedSkips) {
logger.println("Failed Tests exceeded threshold of " + failedFails + " or Skipped Tests exceeded threshold of " + failedSkips + ". Marking build as FAILURE.");
build.setResult(Result.FAILURE);
} else if (failedPercent >= unstableFails || skipPercent >= unstableSkips) {
logger.println("Failed Tests exceeded threshold of " + unstableFails + " or Skipped Tests exceeded threshold of " + unstableSkips + ". Marking build as UNSTABLE.");
build.setResult(Result.UNSTABLE);
}
} else {
if (results.getFailCount() >= failedFails || results.getSkipCount() >= failedSkips) {
logger.println("Failed Tests exceeded threshold of " + failedFails + " or Skipped Tests exceeded threshold of " + failedSkips + ". Marking build as FAILURE.");
build.setResult(Result.FAILURE);
} else if (results.getFailCount() >= unstableFails || results.getSkipCount() >= unstableSkips) {
logger.println("Failed Tests exceeded threshold of " + unstableFails + " or Skipped Tests exceeded threshold of " + unstableSkips + ". Marking build as UNSTABLE.");
build.setResult(Result.UNSTABLE);
}
if (thresholdMode == 1) {
if (results.getFailCount() >= failedFails || results.getSkipCount() >= failedSkips) {
logger.println("Failed Tests exceeded threshold of " + failedFails + " or Skipped Tests exceeded threshold of " + failedSkips + ". Marking build as FAILURE.");
build.setResult(Result.FAILURE);
} else if (results.getFailCount() >= unstableFails || results.getSkipCount() >= unstableSkips) {
logger.println("Failed Tests exceeded threshold of " + unstableFails + " or Skipped Tests exceeded threshold of " + unstableSkips + ". Marking build as UNSTABLE.");
build.setResult(Result.UNSTABLE);
}
} else if (thresholdMode == 2) {
float failedPercent = 100 * results.getFailCount() / (float) results.getTotalCount();
float skipPercent = 100 * results.getSkipCount() / (float) results.getTotalCount();
if (failedPercent >= failedFails || skipPercent >= failedSkips) {
logger.println("Failed Tests exceeded threshold of " + failedFails + " or Skipped Tests exceeded threshold of " + failedSkips + ". Marking build as FAILURE.");
build.setResult(Result.FAILURE);
} else if (failedPercent >= unstableFails || skipPercent >= unstableSkips) {
logger.println("Failed Tests exceeded threshold of " + unstableFails + " or Skipped Tests exceeded threshold of " + unstableSkips + ". Marking build as UNSTABLE.");
build.setResult(Result.UNSTABLE);
}
} else {
Exception e = new RuntimeException("Invalid threshold type: " + thresholdMode);
e.printStackTrace(logger);
}
}
} else {
logger.println("Found matching files but did not find any TestNG results.");
Expand Down
15 changes: 9 additions & 6 deletions src/main/resources/hudson/plugins/testng/Publisher/config.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
</f:entry>
</f:section>

<f:entry field="usePercentage" title="Number/Percentage">
<select name="usePercentage">
<option value="false" selected="true">Number</option>
<option value="true">Percentage</option>
</select>
</f:entry>
<f:entry field="thresholdMode" title="${%Choose your threshold mode:}">
<f:radio name="thresholdMode"
value="1"
checked="${instance.thresholdMode==1 or h.defaultToTrue(instance.thresholdMode)}"/>
<label class="attach-previous">Number of tests</label>
<br/>
<f:radio name="thresholdMode" value="2" checked="${instance.thresholdMode==2}"/>
<label class="attach-previous">Percentage of tests</label>
</f:entry>

</f:advanced>
</j:jelly>
8 changes: 4 additions & 4 deletions src/test/java/hudson/plugins/testng/PublisherCtor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public class PublisherCtor {
private int unstableFails = 1;
private int failedSkips = 1;
private int failedFails = 1;
private boolean usePercentage = false;
private int thresholdMode = 1; //default mode is 1 (number of tests)

public Publisher getNewPublisher() {
return new Publisher(reportFilenamePattern, escapeTestDescp, failureOnFailedTestConfig, escapeExceptionMsg, showFailedBuilds,
unstableSkips, unstableFails, failedSkips, failedFails, usePercentage);
unstableSkips, unstableFails, failedSkips, failedFails, thresholdMode);
}

public PublisherCtor setReportFilenamePattern(String reportFilenamePattern) {
Expand Down Expand Up @@ -68,8 +68,8 @@ public PublisherCtor setFailedFails(int failedFails) {
return this;
}

public PublisherCtor setUsePercentage(boolean usePercentage) {
this.usePercentage = usePercentage;
public PublisherCtor setThresholdType(int thresholdMode) {
this.thresholdMode = thresholdMode;
return this;
}
}
3 changes: 2 additions & 1 deletion src/test/java/hudson/plugins/testng/PublisherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import junit.framework.Assert;
import org.junit.Test;
import org.jvnet.hudson.test.HudsonTestCase;
import hudson.plugins.testng.PublisherCtor;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -91,7 +92,7 @@ public void testBuildAborted() throws Exception {
@Test
public void testRoundTrip() throws Exception {
FreeStyleProject p = createFreeStyleProject();
Publisher before = new Publisher("", false, false, true, false, 0, 0, 0, 0, false);
Publisher before = new Publisher("", false, false, true, false, 0, 0, 0, 0, 1);
p.getPublishersList().add(before);

submit(createWebClient().getPage(p,"configure").getFormByName("config"));
Expand Down

0 comments on commit b483f6e

Please sign in to comment.