简体   繁体   English

使用管道和常规,如何从Jenkins中提取我当前构建的测试结果?

[英]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. 我有一个用在Pipeline构建下运行的Groovy编写的相当完整的构建过程,包括运行单元测试并使用JUnitResultArchiver将测试结果报告给Jenkins。

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. 鉴于Jenkins已经为我解析了这个XML并且有测试结果,我想在构建结束时提取任何和所有测试用例以包含在电子邮件中。

Trying to interact with testResultAction I end up with unclassified method errors. 试图与testResultAction进行交互我最终得到了未分类的方法错误。

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 . 你可以这样做的一种方法是编写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. 在postbuild groovy脚本中,您可以通过jenkins api或filesystem检索管道中执行的所有构建中的所有工件。

Once you have all the information, I would format it nicely into a HTML and inject that into the Email-ext plugin . 一旦掌握了所有信息,我就会很好地将其格式化为HTML并将其注入到Email-ext插件中

So, the steps would be: 那么,步骤将是:

  1. Archive the test results in the test job. 将测试结果存档在测试作业中。 For example, (eg **/surefire/**, **/failsafe/** ) 例如,(例如**/surefire/**, **/failsafe/**
  2. Add a groovy post build step in the pipeline job. 在管道作业中添加一个groovy post构建步骤。
  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. 只需遍历测试作业的归档目录,即可从测试作业中获取测试结果xmls。 For example, reading in C:\\Jenkins\\Jobs\\MyTestJob\\builds\\xxxx\\surefire\\test-results.xml ) 例如,在C:\\Jenkins\\Jobs\\MyTestJob\\builds\\xxxx\\surefire\\test-results.xml读取)
  5. Format it all nicely using HTML and CSS. 使用HTML和CSS很好地格式化它。
  6. Inject that into the Ext email plugin. 将其注入Ext电子邮件插件。 An easy way to do that would be to use the ${FILE,path="pretty.html"} in the email content. 一种简单的方法是在电子邮件内容中使用$ {FILE,path =“pretty.html”}。 Note that you will need to write the file back to slave for this to work. 请注意,您需要将文件写回slave才能使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Jenkins Pipeline发布NUnit测试结果? - How can I publish NUnit test results using Jenkins Pipeline? Jenkins管道,如何将工件从以前的版本复制到当前版本? - Jenkins pipeline, how can I copy artifact from previous build to current build? 如何使用 Jenkins 管道 Groovy 脚本检索当前工作区? - How to retrieve current workspace using Jenkins Pipeline Groovy script? 如何从 jenkins 管道中的 JQL 查询中提取 TestCaseID - How can I extract a TestCaseID from JQL query in jenkins pipeline Jenkins 管道 groovy:如何获得另一个作业构建的工作区 - Jenkins pipeline groovy:how can I get the workspace of another job build 在Groovy和Jenkins管道构建中使用变量 - Using variables in Groovy and Jenkins pipeline build 我可以在 Jenkins 管道中通过普通 Groovy 方法运行并行操作吗? - Can I run parallel actions from a plain Groovy method in my Jenkins pipeline? 在 Jenkins 管道中,如何获取在当前构建和上次成功构建之间更改的文件? - In a Jenkins pipeline, how can I get the files that changed between the current build and last successful build? 如何隔离我的Jenkins管道Groovy共享库类加载器? - How can I isolate my Jenkins pipeline Groovy shared library classloader? 如何使用Groovy在Jenkins中获取当前构建的节点名称 - How to get the current build's node name in jenkins using groovy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM