【问题标题】:Not able to display JUnit tests result in Jenkins Pipeline无法在 Jenkins Pipeline 中显示 JUnit 测试结果
【发布时间】:2016-12-19 20:41:48
【问题描述】:

我有一段 Jenkins 管道代码,我试图在我的角度代码上运行 JUnit。

如果单元测试失败,Jenkins 必须停止流水线。 除了我看不到“最新测试结果”和“测试结果趋势”之外,它正在工作

我正在使用 Jenkins 2.19.1、Jenkins Pipeline 2.4 和 Junit 1.19。这是管道代码:

{
        sh("npm install -g gulp bower")
        sh("npm install")
        sh("bower install")    
        try {
            sh("gulp test")
        } catch (err) {
            step([$class: 'JUnitResultArchiver', testResults: '**/reports/junit/*.xml', healthScaleFactor: 1.0])
            junit '**/reports/junit/*.xml'
            if (currentBuild.result == 'UNSTABLE')
                currentBuild.result = 'FAILURE'
            throw err
        }
    }

知道我做错了什么吗?

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    如果您使用声明式管道,您可以执行以下操作:

    pipeline {
       agent any
       stages {
         stage('Build and Test') {
            steps {
                sh 'build here...'
                sh 'run tests here if you like ...'
            }
         }
       }
       post {
          always {
            junit '**/reports/junit/*.xml'
          }
       } 
    }
    

    这也可以与 html 发布或任何东西一起使用,不需要 finally/catch 等。它将始终存档结果。

    请参阅https://jenkins.io/doc/book/pipeline/syntax/#declarative-pipeline 了解更多信息。

    如果你有一个 clean 目标导致没有测试输出:

      post {
        always {
          junit(
            allowEmptyResults: true,
            testResults: '**/test-reports/*.xml'
          )
        }
    

    【讨论】:

    • 我的 Jenkinsfile 中有这个,它会为每个构建放入 junit 信息。有没有办法从这些数据中获取摘要/趋势?
    • @nroose 该作业包含一个带有测试结果趋势的图表。
    • @michael-neale 感谢allowEmptyResults 的提示!
    【解决方案2】:

    用于发布由 Gradle 构建生成的 JUnit 样式测试结果的 Jenkins 流水线步骤:

      stage('Publish test results') {
          steps {
              junit '**/test-results/test/*.xml'
          }
      } 
    

    【讨论】:

    【解决方案3】:

    我认为我以前尝试做的方式是错误的。我已经改变了我的代码,如下所示,它可以工作:

    {
            sh("npm install -g gulp bower")
            sh("npm install")
            sh("bower install")
            try {
                sh("gulp test")
            } catch (err) {
                if (currentBuild.result == 'UNSTABLE')
                    currentBuild.result = 'FAILURE'
                throw err
            } finally {
                step([$class: 'JUnitResultArchiver', testResults: '**/reports/junit/*.xml', healthScaleFactor: 1.0])
                publishHTML (target: [
                        allowMissing: false,
                        alwaysLinkToLastBuild: false,
                        keepAll: true,
                        reportDir: 'coverage',
                        reportFiles: 'index.html',
                        reportName: "Junit Report"
                ])
            }
        }
    

    【讨论】:

    • 覆盖数据的 PublishHTML 与发布 JUnit 结果不同。
    猜你喜欢
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2021-02-07
    • 1970-01-01
    • 2013-09-10
    • 2017-12-08
    相关资源
    最近更新 更多