我有一个相当完整的构建过程,用Groovy编写,在Pipeline构建下运行,包括运行单元测试并使用JUnitResultArchiver将测试结果报告给Jenkins。
鉴于Jenkins已经为我解析了这个XML并且有测试结果,我想在构建结束时提取任何和所有测试用例以包含在电子邮件中。
尝试与testResultAction交互我最终得到了未分类的方法错误。
任何帮助或示例都将不胜感激!
答案 0 :(得分:9)
结束了整理。这是我写的功能,随意调整以满足您的需求:
@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)
}
答案 1 :(得分:2)
您可以通过编写Postbuild Groovy Script来实现此目的。
在postbuild groovy脚本中,您可以通过jenkins api或文件系统检索管道中执行的所有构建中的所有工件。
获得所有信息后,我会很好地将其格式化为HTML并将其注入Email-ext plugin。
所以,步骤将是:
**/surefire/**, **/failsafe/**
)C:\Jenkins\Jobs\MyTestJob\builds\xxxx\surefire\test-results.xml
)