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.
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.
Trying to interact with testResultAction I end up with unclassified method errors.
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 .
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.
Once you have all the information, I would format it nicely into a HTML and inject that into the Email-ext plugin .
So, the steps would be:
**/surefire/**, **/failsafe/**
) C:\\Jenkins\\Jobs\\MyTestJob\\builds\\xxxx\\surefire\\test-results.xml
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.