【问题标题】:Jenkins JUnit retaining the results even it's already cleaned upJenkins JUnit 保留结果,即使它已经被清理了
【发布时间】:2020-07-29 09:04:05
【问题描述】:

我有一个包含 2 个阶段的 Jenkins 管道,它们都使用 JUnit 清理和计算测试结果。我的问题是,当我现在运行第 2 阶段时,Jenkins JUnit 仍然能够计算第 1 阶段清理后的测试输出目录。这怎么可能?我只想统计第二阶段的测试结果。

           stage('1'){  
             //Insert code to delete test-output directory here
             //Insert build trigger here
                    junit '***/test-output/TEST-*.xml'

                    script {
                        AbstractTestResultAction testResultAction =  currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
                        if (testResultAction != null) {
                            def totalNumberOfTests = testResultAction.totalCount
                            def failedNumberOfTests = testResultAction.failCount
                            def failedDiff = testResultAction.failureDiffString
                            def skippedNumberOfTests = testResultAction.skipCount
                            def passedNumberOfTests = totalNumberOfTests - failedNumberOfTests - skippedNumberOfTests
                            emailTestReport = "Tests Report:\n Passed: ${passedNumberOfTests}; Failed: ${failedNumberOfTests} ${failedDiff}; Skipped: ${skippedNumberOfTests}  out of ${totalNumberOfTests} "
                        }
                    }

                    mail to: 'example@email.com',
                    subject: "Tests are finished: ${currentBuild.fullDisplayName}",
                    body: "Tests are finished  ${env.BUILD_URL}\n  Test Report: ${emailTestReport} "
                }

            }
        }
stage('2'){

           //Insert Code to delete test-output directory here
          //Insert build trigger here
                    junit '***/test-output/TEST-*.xml'

                    script {
                        AbstractTestResultAction testResultAction =  currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
                        if (testResultAction != null) {
                            def totalNumberOfTests = testResultAction.totalCount
                            def failedNumberOfTests = testResultAction.failCount
                            def failedDiff = testResultAction.failureDiffString
                            def skippedNumberOfTests = testResultAction.skipCount
                            def passedNumberOfTests = totalNumberOfTests - failedNumberOfTests - skippedNumberOfTests
                            emailTestReport = "Tests Report:\n Passed: ${passedNumberOfTests}; Failed: ${failedNumberOfTests} ${failedDiff}; Skipped: ${skippedNumberOfTests}  out of ${totalNumberOfTests} "
                        }
                    }

                    mail to: 'example@email.com',
                    subject: "Tests are finished: ${currentBuild.fullDisplayName}",
                    body: "Tests are finished  ${env.BUILD_URL}\n  Test Report: ${emailTestReport} "
                }

            }
        }

【问题讨论】:

    标签: selenium jenkins junit


    【解决方案1】:

    通过调用 junit(...) 步骤,给定的测试结果将被解析并注册到构建运行中。要执行测试结果的“清理”,仅从工作区中删除测试报告文件是没有帮助的。相反,必须从内部构建模型中删除相应的值。 这可以通过从构建中检索AbstractTestResultAction 并设置一个新的(空)TestResult 来实现。试试这样:

    AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
    def listener = Jenkins.get().getItemByFullName(env.JOB_NAME).getBuildByNumber(Integer.parseInt(env.BUILD_NUMBER)).getListener()
    testResultAction.setResult(new TestResult(), listener)
    

    【讨论】:

      猜你喜欢
      • 2010-11-28
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多