当前位置: 首页 > 知识库问答 >
问题:

从项目级分解脚本创建测试报告

元叶秋
2023-03-14

我已经根据测试套件的执行生成了一个报告,它在其中创建了一个文件夹目录并插入了一个显示报告的文件。这是在测试套件级别的Tear多谢脚本中编译的。以下是代码:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
def dataFolder = groovyUtils.projectPath
def failedTestCases = 0
def succeedTestCases = 0
def totalTestCases = 0
def testCaseFailed = ""
def testCaseSucceed = ""
def date = new Date()
def folderTime = date.format("yyyy-MM-dd HH-mm-ss")
def hotelId = context.getProperty('hotelid')
def hotelname = context.getProperty('hotelname')
def hoteltype = context.getProperty('hoteltype')
//def propertyValues = ""
//def correlationid = messageExchange.modelItem.testStep.testCase.testSuite.Project.namegetPropertyValue("correlationid")
//Create a folder directory for the responses
RootResultFolder = dataFolder + "\\Test Reports" + "\\xxx_WebAPI - " + folderTime + "\\"
CreateResultFolder = new File(RootResultFolder)
CreateResultFolder.mkdir()

//context.setProperty("RootResultFolder", RootResultFolder)

def fileName = "WebAPI Test Report.txt"
def rootFolder = RootResultFolder + fileName 
def logFile = new File(rootFolder)



if(logFile.exists())
{

 log.info("Error a file named " + fileName + "already exisits")
}
    else
{
runner.results.each { testCaseResult ->
    def name = testCaseResult.testCase.name
    totalTestCases++
    if(testCaseResult.status.toString() == 'FAILED'){
        failedTestCases ++
        testCaseFailed += "- $name - HAS FAILED \n\n"
       //propertyValues += "hotelid - $hotelid, hotelname - $hotelname, hoteltype - $hoteltype \n\n"
        testCaseResult.results.each{ testStepResults ->
            testStepResults.messages.each() { msg -> log.info msg } 
        }
    }else{
        succeedTestCases ++
        testCaseSucceed += "- $name - SUCCEED \n\n"
        testCaseResult.results.each{ testStepResults ->
        testStepResults.messages.each() { msg -> log.info msg } 
        }
    }
}
}


logFile.write   "TOTAL TEST CASES SUCCEED: $succeedTestCases of $totalTestCases" + "\n\n" +
            testCaseSucceed + "---\n\n" +
            "TOTAL TEST CASES FAILED: $failedTestCases of $totalTestCases" + "\n\n" +
            testCaseFailed + "\n\n"

我实际上想做的是将代码从测试套件层转移到项目层的拆卸脚本中。现在,当我从那里运行代码时,它不会生成文件,我假设我需要放置正确的路径,因为我不是从测试套件到测试用例,而是从项目到测试套件到测试用例到测试步骤。

我的问题实际上是关于语法的,我想在整个项目运行时开发一份报告,它输出以下结果:

    < li >项目名称-是成功还是失败。如果一个套件失败,则项目失败,否则通过 < li >测试套件-取项目中每个测试套件的名称,如果通过,则将“成功”放在测试套件名称旁边,否则将“失败”放在测试套件名称旁边 < li >测试套件中所有测试用例的名称。就像截图中的那样,在已经通过的测试用例旁边显示“成功”,在没有通过的测试用例旁边显示“失败”。 < li >最后是属性值。如果一个测试用例失败了,获取这个失败的测试用例的属性值,这样我们就可以跟踪输入了哪些导致测试失败的值。

有人能帮助我使用相关的语法来执行这些操作吗?这样我就可以将其插入代码并进行操作了?

更新:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
def dataFolder = groovyUtils.projectPath
def date = new Date()
def folderTime = date.format("yyyy-MM-dd HH-mm-ss")

//Create a folder directory for the responses
RootResultFolder = dataFolder + "\\Test Reports" + "\\xxx - " + folderTime + "\\"
CreateResultFolder = new File(RootResultFolder)
CreateResultFolder.mkdir()*/

//context.setProperty("RootResultFolder", RootResultFolder)

def reportFileName = "WebAPI Test Report.txt"
def rootFolder = RootResultFolder + reportFileName 
def logFile = new File(rootFolder)

共有1个答案

慕河
2023-03-14

如果您查看项目的Tear多谢脚本,它显示如下,即soapUI已经初始化的变量。

脚本有问题
所以如果你看一下它,就会发现有运行器变量。在测试套件级别的拆解脚本中也提供了相同的变量。但是,这些是不同对象的实例。OP中使用的脚本是你知道的套件级别,这就是为什么你在结果中看不到的原因。

这是项目级别的拆解脚本和以下行注释。

/**
*
* Below is the TearDown script for SoapUI Project level
* Which create a custom report in a  given file
* Modify the variable "reportFileName" below
*
**/
//Modify the file as needed for report file
//def reportFileName = '/tmp/abctestreport.txt'
//Adding the below as user wants specific directory
//Get the project path

def dataFolder =  new com.eviware.soapui.support.GroovyUtils(context).projectPath

//Create today's date for storing response
def today = new Date().format("yyyy-MM-dd")

def filePrefix = "${dataFolder}/TestReports/xxx_WebAPI_${today}" as String

def fileNamePart = new Date().format("yyyy-MM-dd'T'HH.mm.ss")

//creating filename dynamically.
def reportFileName = "${filePrefix}/xxx_WebAPI_TestReport_${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}"
        if ( caseRunner.status.toString() == 'FAILED' ){
            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> casaeResults = [:]
    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)          
            casaeResults[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) {
    def report = new StringBuffer()
    report.append(drawLine()).append('\n')
    report.append("\t\t\tTest Execution Summary\n")
    report.append(drawLine('-', 60)).append('\n')
    report.append("Project : ${project.name}\n")
    report.append("Result : ${getResult(projectResultHolder.status)}\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
}

//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 ->
        report.append("Name : ${kase}\n")
        report.append("Status : ${getResult(tcReport.status)}\n")
        if (!tcReport.status) {
            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 Cases : ${details.testCaseCount}\n")
        report.append("Cases Passed : ${details.passedCasesCount}\n")
        report.append("Cases Failed: ${details.failedCasesCount}\n")
        report.append(getTestCaseReport(details.casaeResults))
        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))
finalReport.append(getDetailedReport(holder))

def reportFile = new File(reportFileName)
saveToFile(reportFile, finalReport.toString())
 类似资料:
  • 在一个配置了多个应用项目和多个 Library 项目的多项目里,当同时运行所有测试的时候,测试结果整合到一份测试报告中可能是非常有用的。 为了实现这个目的,需要在同一个配置中添加另一个插件。可以通过以下方式添加: buildscript { repositories { jcenter() } dependencies { classpath

  • 昨天,我编写并运行了一个Katalon测试套件,今天,我试图将Katalon与Jenkins集成。我成功地安装了Jenkins,根据这些说明为Katalon测试创建了一个新的工作,但是当我开始构建它时,我得到了失败的构建。 特别是,这是我不断收到的错误消息: 我继续将文件夹结构从我指定的项目目录复制到Jenkins工作区。经过后来的检查,我发现,当Jenkins运行Katalon测试时,文件实际上

  • 一个项目将会自动生成测试运行。默认位置为: build/reports/androidTests 类似于 JUnit 的报告所在位置 build/reports/tests,其它的报告通常位于 build/reports/&lt;plugin>/ 这个路径也可以通过以下方式自定义: android { ... testOptions { reportDir = "$

  • Gradle 项目也可以通过使用多项目配置依赖于其它 Gradle 项目。 多项目配置的实现通常是在一个根项目路径下将所有项目作为子文件夹包含进去。 例如,给定以下项目结构: MyProject/ + app/ + libraries/ + lib1/ + lib2/ 我们可以定义3个项目。Gradle 将会按照以下名字映射它们: :app :libraries:lib1 :l

  • 我有一个多项目分级建筑在当地工作。有一个父级文件build.gradle设置。gradle文件将项目分配给它们各自的分级生成文件: 错误仍然相同,build.gradle文件(父文件)无法识别依赖项任务查看调试语句,找到并识别子生成分级文件:使用生成文件'/var/../LoadRemote/RemoteLoad_build.gradle'计算项目':LoadRemote'。对加载构建文件显示相同

  • 我从他们的github下载了gradle插件测试项目:https://github.com/gradle/gradle/tree/master/subprojects/docs/src/samples/customplugin 我还将gradle更新到了最新版本()。 当我项目时,我得到以下生成错误: > 错误:配置根项目“consumer”时出现问题。 无法解析配置“:classpath”的所有