From b2f43bfe0a3e77aeababdbacc963e1948ec6bf51 Mon Sep 17 00:00:00 2001 From: Aleksi Simell Date: Fri, 27 May 2022 14:30:49 +0300 Subject: [PATCH] Add passed and skipped cases to REST API response (#48) * Add passed and skipped cases to REST API response * Add docstring to functions --- .../plugins/robot/model/RobotResult.java | 79 +++++++++++++++++-- .../plugins/robot/model/RobotSuiteResult.java | 36 ++++++++- 2 files changed, 106 insertions(+), 9 deletions(-) diff --git a/src/main/java/hudson/plugins/robot/model/RobotResult.java b/src/main/java/hudson/plugins/robot/model/RobotResult.java index b013faf..69e0ab9 100755 --- a/src/main/java/hudson/plugins/robot/model/RobotResult.java +++ b/src/main/java/hudson/plugins/robot/model/RobotResult.java @@ -321,22 +321,87 @@ public List getAllFailedCases(){ Collections.sort(allFailedCases, new RobotCaseComparator()); return allFailedCases; } - + + /** + * Get all passed test cases related to result. + * @return list of test case results + */ + public List getAllPassedCases(){ + List allPassedCases = new ArrayList<>(); + for(RobotSuiteResult suite : getSuites()){ + List passedCases = suite.getAllPassedCases(); + allPassedCases.addAll(passedCases); + } + Collections.sort(allPassedCases, new RobotCaseComparator()); + return allPassedCases; + } + + /** + * Get all skipped test cases related to result. + * @return list of test case results + */ + public List getAllSkippedCases(){ + List allSkippedCases = new ArrayList<>(); + for(RobotSuiteResult suite : getSuites()){ + List skippedCases = suite.getAllSkippedCases(); + allSkippedCases.addAll(skippedCases); + } + Collections.sort(allSkippedCases, new RobotCaseComparator()); + return allSkippedCases; + } + + /** + * Get all failed test case names related to result. + * @return list of test case names as strings + */ @Exported public List getFailedCases() { List failedCases = new ArrayList<>(); for (RobotCaseResult robotCaseResult : this.getAllFailedCases()) { - RobotTestObject rto = robotCaseResult.getParent(); - String name = robotCaseResult.getName(); - while (rto != null && !rto.getName().isEmpty()) { - name = rto.getName()+"."+name; - rto = rto.getParent(); - } + String name = this.getCaseName(robotCaseResult); failedCases.add(name); } return failedCases; } + /** + * Get all passed test case names related to result. + * @return list of test case names as strings + */ + @Exported + public List getPassedCases() { + List passedCases = new ArrayList<>(); + for (RobotCaseResult robotCaseResult : this.getAllPassedCases()) { + String name = this.getCaseName(robotCaseResult); + passedCases.add(name); + } + return passedCases; + } + + /** + * Get all skipped test case names related to result. + * @return list of test case names as strings + */ + @Exported + public List getSkippedCases() { + List skippedCases = new ArrayList<>(); + for (RobotCaseResult robotCaseResult : this.getAllSkippedCases()) { + String name = this.getCaseName(robotCaseResult); + skippedCases.add(name); + } + return skippedCases; + } + + private String getCaseName(RobotCaseResult robotCaseResult) { + RobotTestObject rto = robotCaseResult.getParent(); + String name = robotCaseResult.getName(); + while (rto != null && !rto.getName().isEmpty()) { + name = rto.getName()+"."+name; + rto = rto.getParent(); + } + return name; + } + /** * Count the totals in result tree and assign parent action. * @param robotBuildAction The action to be used as the base diff --git a/src/main/java/hudson/plugins/robot/model/RobotSuiteResult.java b/src/main/java/hudson/plugins/robot/model/RobotSuiteResult.java index 7cd17a8..cd3b6d0 100644 --- a/src/main/java/hudson/plugins/robot/model/RobotSuiteResult.java +++ b/src/main/java/hudson/plugins/robot/model/RobotSuiteResult.java @@ -315,16 +315,48 @@ public List getAllChildSuites() { */ public List getAllFailedCases() { List failedCases = new ArrayList<>(); - for(RobotCaseResult caseResult : getCaseResults()){ + for(RobotCaseResult caseResult : getCaseResults()) { if(!caseResult.isPassed() && !caseResult.isSkipped()) failedCases.add(caseResult); } - for(RobotSuiteResult suite : getChildSuites()){ + for(RobotSuiteResult suite : getChildSuites()) { failedCases.addAll(suite.getAllFailedCases()); } Collections.sort(failedCases, new RobotCaseComparator()); return failedCases; } + /** + * Get all passed cases in this suite and its child suites + * @return all passed cases in this suite and its child suites + */ + public List getAllPassedCases() { + List passedCases = new ArrayList<>(); + for(RobotCaseResult caseResult : getCaseResults()) { + if(caseResult.isPassed()) passedCases.add(caseResult); + } + for(RobotSuiteResult suite : getChildSuites()) { + passedCases.addAll(suite.getAllPassedCases()); + } + Collections.sort(passedCases, new RobotCaseComparator()); + return passedCases; + } + + /** + * Get all skipped cases in this suite and its child suites + * @return all skipped cases in this suite and its child suites + */ + public List getAllSkippedCases() { + List skippedCases = new ArrayList<>(); + for(RobotCaseResult caseResult : getCaseResults()) { + if(caseResult.isSkipped()) skippedCases.add(caseResult); + } + for(RobotSuiteResult suite : getChildSuites()) { + skippedCases.addAll(suite.getAllSkippedCases()); + } + Collections.sort(skippedCases, new RobotCaseComparator()); + return skippedCases; + } + /** * Get all cases in this suite and its child suites * @return all cases in this suite and its child suites