@Override public StepExecution start(StepContext context) throws Exception { return new JUnitResultsStepExecution(this, context); }
public static void assertBranchResults(WorkflowRun run, int suiteCount, int testCount, String branchName, String stageName) { assertBranchResults(run, suiteCount, testCount, -1, branchName, stageName, null); }
public static void assertStageResults(WorkflowRun run, int suiteCount, int testCount, String stageName) { assertStageResults(run, suiteCount, testCount, -1, stageName); }
List<FlowNode> enclosingBlocks = JUnitResultsStepExecution.getEnclosingStagesAndParallels(node); PipelineTestDetails pipelineTestDetails = new PipelineTestDetails(); pipelineTestDetails.setNodeId(nodeId); pipelineTestDetails.setEnclosingBlocks(JUnitResultsStepExecution.getEnclosingBlockIds(enclosingBlocks)); pipelineTestDetails.setEnclosingBlockNames(JUnitResultsStepExecution.getEnclosingBlockNames(enclosingBlocks));
@Override protected TestResultSummary run() throws Exception { FilePath workspace = getContext().get(FilePath.class); workspace.mkdirs(); Run<?,?> run = getContext().get(Run.class); TaskListener listener = getContext().get(TaskListener.class); Launcher launcher = getContext().get(Launcher.class); FlowNode node = getContext().get(FlowNode.class); String nodeId = node.getId(); List<FlowNode> enclosingBlocks = getEnclosingStagesAndParallels(node); PipelineTestDetails pipelineTestDetails = new PipelineTestDetails(); pipelineTestDetails.setNodeId(nodeId); pipelineTestDetails.setEnclosingBlocks(getEnclosingBlockIds(enclosingBlocks)); pipelineTestDetails.setEnclosingBlockNames(getEnclosingBlockNames(enclosingBlocks)); try { TestResultAction testResultAction = JUnitResultArchiver.parseAndAttach(step, pipelineTestDetails, run, workspace, launcher, listener); if (testResultAction != null) { // TODO: Once JENKINS-43995 lands, update this to set the step status instead of the entire build. if (testResultAction.getResult().getFailCount() > 0) { run.setResult(Result.UNSTABLE); } return new TestResultSummary(testResultAction.getResult().getResultByNode(nodeId)); } } catch (Exception e) { listener.getLogger().println(e.getMessage()); throw e; } return new TestResultSummary(); }
@Test public void configRoundTrip() throws Exception { SnippetizerTester st = new SnippetizerTester(rule); JUnitResultsStep step = new JUnitResultsStep("**/target/surefire-reports/TEST-*.xml"); st.assertRoundTrip(step, "junit '**/target/surefire-reports/TEST-*.xml'"); step.setAllowEmptyResults(true); st.assertRoundTrip(step, "junit allowEmptyResults: true, testResults: '**/target/surefire-reports/TEST-*.xml'"); step.setHealthScaleFactor(2.0); st.assertRoundTrip(step, "junit allowEmptyResults: true, healthScaleFactor: 2.0, testResults: '**/target/surefire-reports/TEST-*.xml'"); MockTestDataPublisher publisher = new MockTestDataPublisher("testing"); step.setTestDataPublishers(Collections.<TestDataPublisher>singletonList(publisher)); st.assertRoundTrip(step, "junit allowEmptyResults: true, healthScaleFactor: 2.0, testDataPublishers: [[$class: 'MockTestDataPublisher', name: 'testing']], testResults: '**/target/surefire-reports/TEST-*.xml'"); }
public static void assertStageResults(WorkflowRun run, int suiteCount, int testCount, int failCount, String stageName) { FlowExecution execution = run.getExecution(); DepthFirstScanner scanner = new DepthFirstScanner(); FlowNode aStage = scanner.findFirstMatch(execution, stageForName(stageName)); assertNotNull(aStage); assertBlockResults(run, suiteCount, testCount, failCount, aStage); }
public static void assertBranchResults(WorkflowRun run, int suiteCount, int testCount, int failCount, String branchName, String stageName, String innerStageName) { FlowExecution execution = run.getExecution(); DepthFirstScanner scanner = new DepthFirstScanner(); FlowNode aBranch = scanner.findFirstMatch(execution, branchForName(branchName)); assertNotNull(aBranch); TestResult branchResult = assertBlockResults(run, suiteCount, testCount, failCount, aBranch); String namePrefix = stageName + " / " + branchName; if (innerStageName != null) { namePrefix += " / " + innerStageName; } for (CaseResult c : branchResult.getPassedTests()) { assertEquals(namePrefix + " / " + c.getTransformedTestName(), c.getDisplayName()); } }
@Test public void parallelInStage() throws Exception { WorkflowJob j = rule.jenkins.createProject(WorkflowJob.class, "parallelInStage"); FilePath ws = rule.jenkins.getWorkspaceFor(j); FilePath testFile = ws.child("first-result.xml"); testFile.copyFrom(TestResultTest.class.getResource("junit-report-1463.xml")); FilePath secondTestFile = ws.child("second-result.xml"); secondTestFile.copyFrom(TestResultTest.class.getResource("junit-report-2874.xml")); FilePath thirdTestFile = ws.child("third-result.xml"); thirdTestFile.copyFrom(TestResultTest.class.getResource("junit-report-nested-testsuites.xml")); j.setDefinition(new CpsFlowDefinition("stage('first') {\n" + " node {\n" + " parallel(a: { def first = junit(testResults: 'first-result.xml'); assert first.totalCount == 6 },\n" + " b: { def second = junit(testResults: 'second-result.xml'); assert second.totalCount == 1 },\n" + " c: { def third = junit(testResults: 'third-result.xml'); assert third.totalCount == 3 })\n" + " }\n" + "}\n", true )); WorkflowRun r = rule.assertBuildStatus(Result.UNSTABLE, rule.waitForCompletion(j.scheduleBuild2(0).waitForStart())); TestResultAction action = r.getAction(TestResultAction.class); assertNotNull(action); assertEquals(5, action.getResult().getSuites().size()); assertEquals(10, action.getTotalCount()); assertBranchResults(r, 1, 6, "a", "first"); assertBranchResults(r, 1, 1, "b", "first"); assertBranchResults(r, 3, 3, "c", "first"); assertStageResults(r, 5, 10, "first"); }
assertExpectedResults(r, 1, 6, "7"); assertExpectedResults(r, 1, 1, "8"); assertExpectedResults(r, 3, 3, "9"); assertExpectedResults(r, 2, 7, "7", "8"); assertExpectedResults(r, 4, 9, "7", "9");
/** * Get the stage and parallel branch start node IDs (not the body nodes) for this node, innermost first. * @param node A flownode. * @return A nonnull, possibly empty list of stage/parallel branch start nodes, innermost first. */ @Nonnull public static List<FlowNode> getEnclosingStagesAndParallels(FlowNode node) { List<FlowNode> enclosingBlocks = new ArrayList<>(); for (FlowNode enclosing : node.getEnclosingBlocks()) { if (enclosing != null && enclosing.getAction(LabelAction.class) != null) { if (isStageNode(enclosing) || (enclosing.getAction(ThreadNameAction.class) != null)) { enclosingBlocks.add(enclosing); } } } return enclosingBlocks; }
step.getThresholdMode(), new ExtraConfiguration(step.getTestTimeMarginAsLong(), step.isReduceLog())); List<FlowNode> enclosingBlocks = JUnitResultsStepExecution.getEnclosingStagesAndParallels(node); pipelineTestDetails.setEnclosingBlocks(JUnitResultsStepExecution.getEnclosingBlockIds(enclosingBlocks)); pipelineTestDetails.setEnclosingBlockNames(JUnitResultsStepExecution.getEnclosingBlockNames(enclosingBlocks)); xUnitProcessor.process(run, workspace, listener, launcher, step.getTestDataPublishers(), pipelineTestDetails);
assertExpectedResults(r, 1, 6, "7"); assertExpectedResults(r, 1, 1, "8"); assertExpectedResults(r, 2, 7, "7", "8");
assertBranchResults(r, 1, 6, "a", "outer"); assertBranchResults(r, 1, 1, "b", "outer"); assertBranchResults(r, 3, 3, -1, "c", "outer", "d");
assertStageResults(firstRun, 1, 8, 0, "first"); assertStageResults(firstRun, 1, 1, 0, "second"); assertStageResults(secondRun, 1, 8, 3, "first"); assertStageResults(secondRun, 1, 1, 0, "second"); assertStageResults(thirdRun, 1, 8, 3, "first"); assertStageResults(thirdRun, 1, 1, 0, "second"); TestResultAction thirdAction = thirdRun.getAction(TestResultAction.class); assertNotNull(thirdAction);
@Test public void singleStep() throws Exception { WorkflowJob j = rule.jenkins.createProject(WorkflowJob.class, "singleStep"); j.setDefinition(new CpsFlowDefinition("stage('first') {\n" + " node {\n" + " def results = junit(testResults: '*.xml')\n" + // node id 7 " assert results.totalCount == 6\n" + " }\n" + "}\n", true)); FilePath ws = rule.jenkins.getWorkspaceFor(j); FilePath testFile = ws.child("test-result.xml"); testFile.copyFrom(TestResultTest.class.getResource("junit-report-1463.xml")); WorkflowRun r = rule.buildAndAssertSuccess(j); TestResultAction action = r.getAction(TestResultAction.class); assertNotNull(action); assertEquals(1, action.getResult().getSuites().size()); assertEquals(6, action.getTotalCount()); assertExpectedResults(r, 1, 6, "7"); // Case result display names shouldn't include stage, since there's only one stage. for (CaseResult c : action.getPassedTests()) { assertEquals(c.getTransformedTestName(), c.getDisplayName()); } }