[英]Using pipeline & groovy, How can I extract test results from Jenkins for my current build?
I have a fairly complete build process written in Groovy running under a Pipeline build, including running unit tests and reporting the test results back to Jenkins using JUnitResultArchiver. 我有一个用在Pipeline构建下运行的Groovy编写的相当完整的构建过程,包括运行单元测试并使用JUnitResultArchiver将测试结果报告给Jenkins。
Given that Jenkins has parsed that XML for me and has the test results, I would like to extract any and all test cases at the end of the build for inclusion in an email. 鉴于Jenkins已经为我解析了这个XML并且有测试结果,我想在构建结束时提取任何和所有测试用例以包含在电子邮件中。
Trying to interact with testResultAction I end up with unclassified method errors. 试图与testResultAction进行交互我最终得到了未分类的方法错误。
Any help or examples would be appreciated! 任何帮助或示例将不胜感激!
Ended up getting this sorted out. 结束了整理。 Here's the function I wrote, feel free to tweak to suit your needs:
这是我写的功能,随时调整以满足您的需求:
@NonCPS
def reportOnTestsForBuild() {
def build = manager.build
println("Build Number: ${build.number}")
if (build.getAction(hudson.tasks.junit.TestResultAction.class) == null) {
println("No tests")
return ("No Tests")
}
// The string that will contain our report.
String emailReport;
emailReport = "URL: ${env.BUILD_URL}\n"
def testResults = build.getAction(hudson.tasks.junit.TestResultAction.class).getFailCount();
def failed = build.getAction(hudson.tasks.junit.TestResultAction.class).getFailedTests()
println("Failed Count: ${testResults}")
println("Failed Tests: ${failed}")
def failures = [:]
def result = build.getAction(hudson.tasks.junit.TestResultAction.class).result
if (result == null) {
emailReport = emailReport + "No test results"
} else if (result.failCount < 1) {
emailReport = emailReport + "No failures"
} else {
emailReport = emailReport + "overall fail count: ${result.failCount}\n\n"
failedTests = result.getFailedTests();
failedTests.each { test ->
failures.put(test.fullDisplayName, test)
emailReport = emailReport + "\n-------------------------------------------------\n"
emailReport = emailReport + "Failed test: ${test.fullDisplayName}\n" +
"name: ${test.name}\n" +
"age: ${test.age}\n" +
"failCount: ${test.failCount}\n" +
"failedSince: ${test.failedSince}\n" +
"errorDetails: ${test.errorDetails}\n"
}
}
return (emailReport)
}
One way you could do this is by writing Postbuild Groovy Script . 你可以这样做的一种方法是编写Postbuild Groovy Script 。
In a postbuild groovy script, you can retrieve all the artifacts from all the builds executed in the pipeline via the jenkins api or filesystem. 在postbuild groovy脚本中,您可以通过jenkins api或filesystem检索管道中执行的所有构建中的所有工件。
Once you have all the information, I would format it nicely into a HTML and inject that into the Email-ext plugin . 一旦掌握了所有信息,我就会很好地将其格式化为HTML并将其注入到Email-ext插件中 。
So, the steps would be: 那么,步骤将是:
**/surefire/**, **/failsafe/**
) **/surefire/**, **/failsafe/**
) C:\\Jenkins\\Jobs\\MyTestJob\\builds\\xxxx\\surefire\\test-results.xml
) C:\\Jenkins\\Jobs\\MyTestJob\\builds\\xxxx\\surefire\\test-results.xml
读取)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.