Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 397
Mike Kingsbury Avatar asked Aug 16 '16 15:08

Mike Kingsbury


People also ask

What is pipeline function?

The purpose of the pipeline is to assemble several steps that can be cross-validated together while setting different parameters. For this, it enables setting parameters of the various steps using their names and the parameter name separated by a '__' , as in the example below.

How do you use pipeline in Python?

Pandas pipeline feature allows us to string together various user-defined Python functions in order to build a pipeline of data processing. There are two ways to create a Pipeline in pandas. By calling . pipe() function and by importing pdpipe package.

How do I use Jenkins pipeline?

Click the New Item menu within Jenkins. Provide a name for your new item (e.g. My-Pipeline) and select Multibranch Pipeline. Click the Add Source button, choose the type of repository you want to use and fill in the details. Click the Save button and watch your first Pipeline run.

What are the 3 types of pipelines in Jenkins?

Different Types of Jenkins CI/CD Pipelines. Scripted Pipeline. Declarative Pipeline.


2 Answers

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)
}
like image 168
Mike Kingsbury Avatar answered Sep 28 '22 17:09

Mike Kingsbury


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, (e.g. **/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.
like image 45
Daniel Omoto Avatar answered Sep 28 '22 16:09

Daniel Omoto