无法在Jenkins Pipeline中显示JUnit测试结果

13

我有一段Jenkins管道代码,其中我正在尝试在我的angular代码上运行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
        }
    }

我做错了什么?

4个回答

31

如果您使用声明式流水线,您可以这样做:

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

如果您有一个不产生测试输出的干净目标:

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

我在我的Jenkinsfile中有这个,它为每个构建放置了junit信息。有没有一种方法可以从这些数据中获取摘要/趋势? - nroose
@nroose 这份工作包含了一张测试结果趋势图。 - t0r0X
1
@michael-neale 感谢您提供 allowEmptyResults 的提示! - t0r0X
应该是 '**/test-results/test/*.xml' - Michel Jung

7

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

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

1
这似乎不再起作用了,未知的阶段部分“junit”。 - user4433284
可能需要特定的JUnit插件才能在DSL中启用junit关键字 - https://jenkins.io/doc/pipeline/steps/junit/#-junit-archive-junit-formatted-test-results - Cinderhaze

4
我认为我之前尝试的方式是错误的。我已经修改了我的代码如下,并且它可以工作:
{
        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"
            ])
        }
    }

3
发布覆盖率数据的PublishHTML与发布JUnit测试结果不是同一件事情。 - BitwiseMan

1
在你的代码中,你只有在测试失败时才会生成 JUnit 报告,这可能不是你想要的。相反,请使用 finally 来始终发布这些测试:
   {
        sh("npm install -g gulp bower")
        sh("npm install")
        sh("bower install")    
        try {
            sh("gulp test")
        } finally {
            step([$class: 'JUnitResultArchiver', testResults: '**/reports/junit/*.xml', healthScaleFactor: 1.0])
            junit '**/reports/junit/*.xml'
        }
    }

我更喜欢使用finally而不是将其放在post中,因为这样可以将相关的内容放在一起。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接