Jenkins构建测试用例使用groovy脚本传递失败计数

时间:2014-07-10 13:41:28

标签: groovy junit jenkins hudson

我想使用groovy脚本获取构建的Total TestCase PASS和FAIL计数。我正在使用Junit测试结果。我正在使用Multiconfiguration项目,那么有没有办法在每个配置的基础上找到这些信息?

4 个答案:

答案 0 :(得分:3)

如果你使用插件https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin,你可以直接从构建中访问Jenkins TestResultAction,即。

import hudson.model.*

def build = manager.build

int total = build.getTestResultAction().getTotalCount()
int failed = build.getTestResultAction().getFailCount()
int skipped = build.getTestResultAction().getSkipCount()
// can also be accessed like build.testResultAction.failCount

manager.listener.logger.println('Total: ' + total)
manager.listener.logger.println('Failed: ' + failed)
manager.listener.logger.println('Skipped: ' + skipped)
manager.listener.logger.println('Passed: ' + (total - failed - skipped))

其他TestResultAction方法/属性http://javadoc.jenkins-ci.org/hudson/tasks/test/AbstractTestResultAction.html

的API

如果您想从另一个作业访问矩阵构建,您可以执行以下操作:

def job = Jenkins.instance.getItemByFullName('MyJobName/MyAxisName=MyAxisValue');
def build = job.getLastBuild()
...

答案 1 :(得分:1)

我认为你可以用这样的东西做到这一点:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

File fXmlFile = new File("junit-tests.xml");

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();

println("Total : " + doc.getDocumentElement().getAttribute("tests"))
println("Failed : " +doc.getDocumentElement().getAttribute("failures"))
println("Errors : " +doc.getDocumentElement().getAttribute("errors"))

答案 2 :(得分:1)

对于Pipeline(工作流程)作业类型,逻辑与AlexS对大多数其他作业类型的回答略有不同:

build.getActions(hudson.tasks.junit.TestResultAction).each {action ->
    action.getTotalCount()
    action.getFailCount()
    action.getSkipCount()
}

(见http://hudson-ci.org/javadoc/hudson/tasks/junit/TestResultAction.html

Pipeline个作业没有getTestResultAction()方法。

我使用这种逻辑消除歧义:

if (build.respondsTo('getTestResultAction')) {
   //normal logic, see answer by AlexS     

} else {
   // pipeline logic above
}

答案 3 :(得分:0)

我还收集了junit xml测试结果,并使用Groovy post-build plugin https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin来检查通过/失败/跳过计数。在groovy post-build脚本之前,确保构建后操作的顺序具有harvest junit xml。

此示例脚本显示如何获取测试结果,使用结果计数设置构建描述以及VERSION.txt文件中的版本信息以及如何在跳过所有测试的情况下将整体构建结果更改为UNSTABLE。

def currentBuild = Thread.currentThread().executable
// must be run groovy post-build action AFTER harvest junit xml  
testResult1 = currentBuild.testResultAction

currentBuild.setDescription(currentBuild.getDescription() + "\n pass:"+testResult1.result.passCount.toString()+", fail:"+testResult1.result.failCount.toString()+", skip:"+testResult1.result.skipCount.toString())

// if no pass, no fail all skip then set result to unstable
if (testResult1.result.passCount == 0 && testResult1.result.failCount == 0 && testResult1.result.skipCount > 0) {
   currentBuild.result = hudson.model.Result.UNSTABLE
}

currentBuild.setDescription(currentBuild.getDescription() + "\n" + currentBuild.result.toString())

def ws = manager.build.workspace.getRemote()
myFile = new File(ws + "/VERSION.txt")
desc = myFile.readLines()
currentBuild.setDescription(currentBuild.getDescription() + "\n" + desc)