首页 > 解决方案 > Jenkins JUnit 保留结果,即使它已经被清理了

问题描述

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

           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} "
                }

            }
        }

标签: seleniumjenkinsjunit

解决方案


通过调用 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)

推荐阅读