简体   繁体   中英

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.

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:

  1. Archive the test results in the test job. For example, (eg **/surefire/**, **/failsafe/** )
  2. Add a groovy post build step in the pipeline job.
  3. Gather the build numbers of the jobs kicked off during the workflow. One way you can do it is by simply parsing them out of the build's console output.
  4. Get the test result xmls from the test jobs by simply traversing the archive directory of the test jobs. For example, reading in C:\\Jenkins\\Jobs\\MyTestJob\\builds\\xxxx\\surefire\\test-results.xml )
  5. Format it all nicely using HTML and CSS.
  6. Inject that into the Ext email plugin. An easy way to do that would be to use the ${FILE,path="pretty.html"} in the email content. Note that you will need to write the file back to slave for this to work.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM