/** * Serves the testCoverage badge image. TO DO * @param req * @param rsp * @param job * @return */ @SuppressWarnings("rawtypes") public HttpResponse doTestIcon(StaplerRequest req, StaplerResponse rsp, @QueryParameter String job) { Job<?, ?> project = getProject(job); Integer testPass = null; Integer testTotal = null; if (project.getLastCompletedBuild() != null) { AbstractTestResultAction testAction = project.getLastCompletedBuild().getAction(AbstractTestResultAction.class); if(testAction != null){ int total = testAction.getTotalCount(); int pass = total - testAction.getFailCount() - testAction.getSkipCount(); testTotal = total; testPass = pass; } } return iconResolver.getTestResultImage(testPass, testTotal); }
public MessageBuilder appendTestSummary() { AbstractTestResultAction<?> action = this.build .getAction(AbstractTestResultAction.class); if (action != null) { int total = action.getTotalCount(); int failed = action.getFailCount(); int skipped = action.getSkipCount(); message.append("\nTest Status:\n"); message.append("\tPassed: " + (total - failed - skipped)); message.append(", Failed: " + failed); message.append(", Skipped: " + skipped); } else { message.append("\nNo Tests found."); } return this; }
/** * Construct a FlakyTestResultAction object with AbstractBuild and BuildListener * * @param build this build * @param listener listener of this build */ public FlakyTestResultAction(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { this.build = build; // TODO consider the possibility that there is >1 such action AbstractTestResultAction action = build.getAction(AbstractTestResultAction.class); if (action != null) { Object latestResult = action.getResult(); if (latestResult != null && latestResult instanceof TestResult) { FlakyTestResult flakyTestResult = launcher.getChannel().call(new FlakyTestResultCollector((TestResult) latestResult)); flakyTestResult.freeze(action, build); FlakyRunStats stats = new FlakyRunStats(flakyTestResult.getTestFlakyStatsMap()); setFlakyRunStats(stats, listener); } } else { logger.log(Level.WARNING, "No test result found, please publish junit report first"); } }
public static List<TestResult> getResults(AbstractBuild<?, ?> build) { if (build != null) { List<TestResult> result = new ArrayList<>(); AbstractTestResultAction resultAction = build.getAction(AbstractTestResultAction.class); if (resultAction != null) { result.add(new TestResult( resultAction.getDisplayName(), build.getUrl() + resultAction.getUrlName(), resultAction.getFailCount(), resultAction.getSkipCount(), resultAction.getTotalCount())); } return result; } return Collections.emptyList(); }
@Test public void testGetTestResult() { AbstractBuild<?, ?> build = mock(AbstractBuild.class); AggregatedTestResultAction tests = mock(AggregatedTestResultAction.class); when(build.getAction(AbstractTestResultAction.class)).thenReturn(tests); when(tests.getDisplayName()).thenReturn("Test Result"); when(tests.getFailCount()).thenReturn(1); when(tests.getSkipCount()).thenReturn(0); when(tests.getTotalCount()).thenReturn(11); List<TestResult> result = TestResult.getResults(build); assertNotNull(result); assertEquals(1, result.get(0).getFailed()); assertEquals(0, result.get(0).getSkipped()); assertEquals(11, result.get(0).getTotal()); assertNotNull(result.get(0).getUrl()); assertNotNull(result.get(0).getName()); }
public MessageBuilder appendTestSummary(boolean includeFailedTests) { AbstractTestResultAction<?> action = this.build .getAction(AbstractTestResultAction.class); if (action != null) { int total = action.getTotalCount(); int failed = action.getFailCount(); int skipped = action.getSkipCount(); message.append("\n"); message.append("<b>Test Status:</b>\n"); message.append("Passed: " + (total - failed - skipped)); message.append(", Failed: " + failed); message.append(", Skipped: " + skipped); if(includeFailedTests && failed > 0){ message.append("\n<b>Failed Tests:</b>\n"); List<? extends TestResult> failedTests = action.getFailedTests(); for(int i = 0; i<failedTests.size();i++){ TestResult result = failedTests.get(i); String testName = result.getName(); if(testName.length() > 60) { String[] splittedTestName = testName.split("\\."); testName = ""; for(int j = splittedTestName.length - 1;j>=0;j--){ if(testName.length() + splittedTestName[j].length() + 1 > 60) break; testName = splittedTestName[j] + "." + testName; } testName = testName.substring(0,testName.length()-1); } message.append(escape(testName + "\n")); } } } else { message.append("\nNo Tests found."); } return this; }
public Point[] generate() { // Build is not finished when running with pipelines. Duration must be calculated manually long startTime = build.getTimeInMillis(); long currTime = System.currentTimeMillis(); long dt = currTime - startTime; // Build is not finished when running with pipelines. Set build status as unknown and ordinal // as something not predefined String result; int ordinal; if (build.getResult() == null) { result = "?"; ordinal = 5; } else { result = build.getResult().toString(); ordinal = build.getResult().ordinal; } Point.Builder point = buildPoint(measurementName("jenkins_data"), customPrefix, build); point.addField(BUILD_TIME, build.getDuration() == 0 ? dt : build.getDuration()) .addField(BUILD_STATUS_MESSAGE, build.getBuildStatusSummary().message) .addField(BUILD_RESULT, result) .addField(BUILD_RESULT_ORDINAL, ordinal) .addField(BUILD_IS_SUCCESSFUL, ordinal < 2 ? true : false) .addField(PROJECT_BUILD_HEALTH, build.getParent().getBuildHealth().getScore()) .addField(PROJECT_LAST_SUCCESSFUL, getLastSuccessfulBuild()) .addField(PROJECT_LAST_STABLE, getLastStableBuild()); if(hasTestResults(build)) { point.addField(TESTS_FAILED, build.getAction(AbstractTestResultAction.class).getFailCount()); point.addField(TESTS_SKIPPED, build.getAction(AbstractTestResultAction.class).getSkipCount()); point.addField(TESTS_TOTAL, build.getAction(AbstractTestResultAction.class).getTotalCount()); } return new Point[] {point.build()}; }
public static String defaultBitbucketBuildDescriptionFromBuild(Run<?, ?> build) { AbstractTestResultAction testResult = build.getAction(AbstractTestResultAction.class); String description = ""; if (testResult != null) { int passedCount = testResult.getTotalCount() - testResult.getFailCount(); description = passedCount + " of " + testResult.getTotalCount() + " tests passed"; } return description; }
@Override public String evaluate(AbstractBuild<?, ?> build, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException { AbstractTestResultAction<?> testResultContainer = build.getAction(AbstractTestResultAction.class); StringBuilder testResult = new StringBuilder(); if (testResultContainer != null) { String testResultDescription = testResultContainer.getBuildHealth().getDescription(); testResult.append(testResultDescription); Integer failedUnitTests = testResultContainer.getFailedTests().size(); if (failedUnitTests > 0) { testResult.append("\nFailures:"); Integer processedUnitTests = 0; for (TestResult testResultItem : testResultContainer.getFailedTests()) { testResult.append(Constants.LINE_SEPARATOR).append("\n").append(testResultItem.getFullName()); if ((null != testResultItem.getErrorDetails()) && !testResultItem.getErrorDetails().trim().isEmpty()) { testResult.append("\n-------- Message --------\n").append(testResultItem.getErrorDetails().trim()); } if ((null != testResultItem.getStderr()) && !testResultItem.getStderr().trim().isEmpty()) { testResult.append("\n-------- Stacktrace --------\n").append(testResultItem.getStderr().trim()); } processedUnitTests++; if (processedUnitTests.equals(failedUnitTests)) { testResult.append(Constants.LINE_SEPARATOR); } } } } return testResult.toString(); }
@Override public TestResultAction.Data getTestData(AbstractBuild<?, ?> abstractBuild, Launcher launcher, BuildListener buildListener, TestResult testResult) throws IOException, InterruptedException { FlakyTestResult flakyTestResult = launcher.getChannel().call(new FlakyTestResultCollector(testResult)); // TODO consider the possibility that there is >1 such action flakyTestResult.freeze(abstractBuild.getAction(AbstractTestResultAction.class), abstractBuild); return new JUnitFlakyTestData(flakyTestResult); }
@Test public void testGetTestResultFreeStyleBuild() { FreeStyleBuild build = mock(FreeStyleBuild.class); TestResultAction action = mock(TestResultAction.class); when(build.getAction(AbstractTestResultAction.class)).thenReturn(action); List<TestResult> result = TestResult.getResults(build); assertNotNull(result); assertEquals(1, result.size()); }
private boolean hasTestResults(Run<?, ?> build) { return build.getAction(AbstractTestResultAction.class) != null; }
@Override public void setParentAction(AbstractTestResultAction action) { this.parentAction = action; tally(); // I want to be sure to inform our children when we get an action. }
@Override public AbstractTestResultAction getParentAction() { return this.parentAction; }
@Override public Data getTestData(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, TestResult testResult) { Data data = new Data(build); MailNotifier notifier = new MailNotifier(listener); for (SuiteResult suite : testResult.getSuites()) { for (CaseResult result : suite.getCases()) { QuarantineTestAction previousAction = null; CaseResult previous = result.getPreviousResult(); AbstractBuild<?, ?> previousBuild = build.getPreviousCompletedBuild(); if (previous != null) { previousAction = previous.getTestAction(QuarantineTestAction.class); } // no immediate predecessor (e.g. because job failed or did not // run), try and go back in build history while (previous == null && previousBuild != null) { if (previousBuild.getAction(AbstractTestResultAction.class) != null) { hudson.tasks.test.TestResult tr = null; try { tr = previousBuild.getAction(AbstractTestResultAction.class).findCorrespondingResult( result.getId()); } catch (Exception e){ listener.getLogger().println("could not find result for id " + result.getId() + " in build " + previousBuild + ": " + e.getMessage()); } if (tr != null) { listener.getLogger().println("found " + tr.getDisplayName() + " in build " + previousBuild); previousAction = tr.getTestAction(QuarantineTestAction.class); break; } } else { listener.getLogger().println("build " + previousBuild + " does not have test results"); } previousBuild = previousBuild.getPreviousCompletedBuild(); } if (previousAction != null && previousAction.isQuarantined()) { QuarantineTestAction action = new QuarantineTestAction(data, result.getId()); action.quarantine(previousAction); data.addQuarantine(result.getId(), action); // send email if failed if (!result.isPassed()) { notifier.addResult(result, action); } } } } notifier.sendEmails(); return data; }
@Override public void setParentAction(@SuppressWarnings("rawtypes") AbstractTestResultAction action) { this.parentAction = (UnitTestResultAction) action; }