Using pipe & groovy, How can I extract test results from Jenkins for my current build?

I have a fairly complete build process written in Groovy, working under the Pipeline assembly, including running unit tests and passing test results back to Jenkins using JUnitResultArchiver.

Given that Jenkins parsed this XML for me and got the test results, I would like to extract all and all test cases at the end of the assembly for inclusion in the email.

Trying to interact with testResultAction, I get unclassified method errors.

Any help or examples would be appreciated!

+4
source share
2 answers

, . , , :

@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)
}
+6

- Postbuild Groovy Script.

postbuild Groovy script , , api jenkins.

, HTML -ext.

, :

  • . , (, **/surefire/**, **/failsafe/**)
  • Groovy post build .
  • , . - .
  • xmls , . , C:\Jenkins\Jobs\MyTestJob\builds\xxxx\surefire\test-results.xml)
  • HTML CSS.
  • Ext . ${FILE, path = "pretty.html" } . , , .
+2

Source: https://habr.com/ru/post/1651422/


All Articles