Forum Discussion

VigneshR32's avatar
VigneshR32
Senior Member
3 years ago

In Ready API - Logging the pass and fail count of test steps excluding assertions

In Jenkins console, logging the test steps pass and fail counts and categorizing them using endpoints. Below is the format expected

 

EndpointTotalPassFail
Login1082
Create user321200121

 

Could some one help me with scripts to achieve it.

  • JoostDG's avatar
    JoostDG
    Frequent Contributor

    Hi VigneshR32 ,

     

    I found a script somewhere online, which I applied and adjusted a bit in one of my projects some time ago. The script writes the test results of a project run via a teardown script to a file. Perhaps not fully what you are looking for, but it can give some clues to achieve your goal.

     

    All credits to the original uploader. 

     

    /*
    * Below is the TearDown script for SoapUI Project level
    * Which create a custom report in a  given file
    * Modify the variable "reportFileName" below
    *
    */
    //Get the project path (including the project foled, i.e. Wed Jan 15 13:13:40 UTC 2020: INFO: ... stored in file at location C:\git\readyAPIProjects/yourProjectName
    def dataFolder = context.expand( '${#Project#projectpath}' )
    //Create today's date for storing response
    LocalDateTime now = LocalDateTime.now()
    DateTimeFormatter wantedDateFormat = DateTimeFormatter.ofPattern("EEE_dd-MM-YYYY").withLocale(Locale.US)
    DateTimeFormatter wantedDateTimeFormat = DateTimeFormatter.ofPattern("dd-MM-yyyy'T'HH.mm.ss").withLocale(Locale.US)
    String today = wantedDateFormat.format(now)
    String fileNamePart = wantedDateTimeFormat.format(now)
    def filePrefix = "${dataFolder}/ReadyAPI_TestReports/${today}"
    
    //creating filename dynamically.
    def reportFileName = "${filePrefix}/"+project.getActiveEnvironment().getName()+"/"+project.getName()+"_${fileNamePart}.txt" as String
    
    //NOTE: Not required to edit beyond this point
    /**
    * This class holds the test case details
    **/
    class TestCaseResultHolder {
        def log
        Map<String, String> properties = [:]
        boolean status
    
        def createProperties(testCase) {
            testCase.getPropertyNames().each { key ->
                properties[key] = testCase.getPropertyValue(key)
            }       
        }
    
        def getCaseResult(caseRunner, caseName) {
            //log.info "Checking test case status ${caseName}"
            //log.info "Checking test case status ${caseRunner.status}"
            if ( caseRunner.status.toString() == 'FAIL' ){
               // log.error "Test case $caseName has failed"
                for ( stepResult in caseRunner?.results ){
                    stepResult.messages.each() { msg -> log.info msg }
                }
                return false
            } else {
               // log.info "${caseName} is passed"
            }
            true
        }
    
        def buildCaseResult(caseRunner, caseName) {
            status = getCaseResult(caseRunner, caseName)
            if (!status) {
                //createProperties(caseRunner.testCase)
            }
        }
    
    }
    
    /**
    * This class holds the test suite details
    **/
    class SuiteResultsHolder {
    
        def log
        Map<String, TestCaseResultHolder> caseResults = [:]
        int testCaseCount = 0
        int passedCasesCount = 0
        int failedCasesCount = 0
    
        def buildSuiteResults(suiteRunner, suiteName)
        {      
            //log.info "Building results of test suite ${suiteName}"
            for ( caseRunner in suiteRunner?.results ) {
                def caseName = caseRunner.testCase.name
                testCaseCount++
                def tcHolder = new TestCaseResultHolder(log: log)
                tcHolder.buildCaseResult(caseRunner, caseName)          
                caseResults[caseName] = tcHolder
                if (tcHolder.status) {
                    passedCasesCount++
                } else {
                    failedCasesCount++
                }
            }
        }
    
        def getStatus() {
            (0 < failedCasesCount) ? false : true
        }
    
    }
    
    /**
    * This class holds the project details
    **/
    class ProjectResultsHolder {
    
        def log
        Map<String, SuiteResultsHolder> suiteResults = [:]
        int suiteCount = 0
        int passedSuitecount = 0
        int failedSuiteCount = 0
    
        def buildProjectResults(projectRunner, projectName) {
            //log.info "Building results of test project ${projectName}"          
            for(suiteRunner in projectRunner?.results) {
                def suiteName =  suiteRunner.testSuite.name
                suiteCount++
                def suiteResultsHolder = new SuiteResultsHolder(log: log)
                suiteResultsHolder.buildSuiteResults(suiteRunner, suiteName)
                suiteResults[suiteName] = suiteResultsHolder
                if (suiteResultsHolder.status) {
                    passedSuitecount++
                } else {
                    failedSuiteCount++
                }
            }
        }
    
        def getStatus() {
            (0 < failedSuiteCount) ? false : true
        }
    
    }
    
    //Get the status string based on boolean
    def getResult(status){ status == true ? 'SUCCEED' : 'FAILED'}
    
    //Draws a line
    def drawLine(def letter = '=', def count = 70) { letter.multiply(count)}
    
    //Gets the summary report
    def getSummaryReport(project, projectResultHolder,context, runner, fileNamePart) {
    	def report = new StringBuffer()
    	def versionAPPyourProjectName = context.expand( '${#Project#versionAPPyourProjectName}' )
    	def versionAPPyourProjectNameDeployDate = context.expand( '${#Project#versionAPPyourProjectNameDeployDate}' )
    	def versionBFF = context.expand( '${#Project#versionBFF}' )
    	def versionBFFDeployDate = context.expand( '${#Project#versionBFFDeployDate}' )
    	def versionyourProjectNameApi = context.expand( '${#Project#versionyourProjectNameApi}' )
    	def versionyourProjectNameApiDeployDate = context.expand( '${#Project#versionyourProjectNameApiDeployDate}' )
    	def versionINTApiX = context.expand( '${#Project#versionINTApiX}' )
    	def versionINTApiY = context.expand( '${#Project#versionINTApiY}' )
    	def versionINTAPIZyourProjectName = context.expand( '${#Project#versionINTAPIZyourProjectName}' )
    	def timetakeninms = runner.timeTaken
    	String timeTakenInMinutes = String.format("%d min, %d sec", 
        TimeUnit.MILLISECONDS.toMinutes(timetakeninms),
        TimeUnit.MILLISECONDS.toSeconds(timetakeninms) - 
        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timetakeninms))
    )
    
        report.append(drawLine()).append('\n')
        report.append("\t\t\tTest Execution Summary\n")
        report.append(drawLine('-', 60)).append('\n')
        report.append("Project : ${project.name} on "+project.getActiveEnvironment().getName()+" "+ fileNamePart.toString() +"\n")
        report.append("Result : ${getResult(projectResultHolder.status)}\n")
        report.append("TimeTaken : "+timeTakenInMinutes+ " \n")
        report.append("Total test suites executed: ${projectResultHolder.suiteCount}\n")
        report.append("Test suites passed: ${projectResultHolder.passedSuitecount}\n")
        report.append("Test suites failed: ${projectResultHolder.failedSuiteCount}\n")
        report.append(drawLine()).append('\n')
        report.append("versionAPPyourProjectName : $versionAPPyourProjectName\n")
        report.append("versionAPPyourProjectNameDeployDate : $versionAPPyourProjectNameDeployDate\n")
        report.append("versionBFF : $versionBFF\n")
        report.append("versionBFFDeployDate : $versionBFFDeployDate\n")
        report.append("versionyourProjectNameApi : $versionyourProjectNameApi\n")
        report.append("versionyourProjectNameApiDeployDate : $versionyourProjectNameApiDeployDate\n")
        report.append("versionINTApiX : $versionINTApiX\n")
        report.append("versionINTApiY : $versionINTApiY\n")    
        report.append("versionINTAPIZyourProjectName : $versionINTAPIZyourProjectName\n")    
        report.append(drawLine()).append('\n')
        report
    }
    
    //Gets the test case report
    def getTestCaseReport(testCaseReport) {
        def report = new StringBuffer()
        report.append(drawLine('-', 60)).append('\n')
        report.append("\t\tTest Case Details:\n")
        report.append(drawLine('-', 60)).append('\n')
        testCaseReport.each { kase, tcReport ->
            if (!tcReport.status) {
            	  report.append("Name : ${kase}\n")
            	  report.append("Status : ${getResult(tcReport.status)}\n")
                // report.append("Properties : ${tcReport.properties.toString()}\n")
            }
        }
        report
    }
    
    //Get the detailed report
    def getDetailedReport(projectResultHolder) {
        def report = new StringBuffer()
        report.append(drawLine()).append('\n')
        report.append("\t\t\tTest Execution Detailed Report\n")
        report.append(drawLine()).append('\n')
        projectResultHolder.suiteResults.each { suite, details ->
            report.append("Test Suite : ${suite}\n")
            report.append("Result : ${getResult(details.status)}\n")
            report.append("Total Test Cases : ${details.testCaseCount}\n")
            //report.append("Cases Passed : ${details.passedCasesCount}\n")
            report.append("Cases Failed: ${details.failedCasesCount}\n")
            report.append(getTestCaseReport(details.caseResults))
            report.append(drawLine()).append('\n')
            report.append(drawLine()).append('\n')
        }
        report
    }
    
    //Save the contents to a file
    def saveToFile(file, content) {
        if (!file.parentFile.exists()) {
            file.parentFile.mkdirs()
            log.info "Directory did not exist, created"
        }
        file.write(content) 
        assert file.exists(), "${file.name} not created"
    }
    
    def holder = new ProjectResultsHolder(log: log)
    holder.buildProjectResults(runner, project.name)
    
    def finalReport = new StringBuffer()
    finalReport.append(getSummaryReport(project, holder, context, runner, fileNamePart))
    finalReport.append(getDetailedReport(holder))
    
    def reportFile = new File(reportFileName)
    saveToFile(reportFile, finalReport.toString())