Reputation: 936
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!
Upvotes: 12
Views: 27861
Reputation: 16971
build.testResultAction
should give you access to test results.
For example, here's how I set Email-ext subject to include tests count.
Add to "Pre-send Script":
import javax.mail.Message
def testResult = build.testResultAction
if ( testResult != null ) {
def testsTotal = testResult.totalCount
def testsFailed = testResult.failCount
def testsSkipped = testResult.skipCount
def testsPassed = testsTotal - testsFailed - testsSkipped
def subject = build.externalizableId + " : Tests passed " + testsPassed + ", out of " + testsTotal
msg.setSubject(subject)
}
Upvotes: 0
Reputation: 936
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)
}
Upvotes: 14
Reputation: 2481
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
)Upvotes: 2