如果测试数量减少,Jenkins 设置构建失败?

Jenkins set build failed if number of tests decreases?

如果 jenkins 中的测试数量减少,是否有办法将测试标记为 运行 失败。我们在 JUnit 中有一份测试报告。 由于某些原因,有时测试的数量会减少。通常这是严重错误。有没有办法说詹金斯在这种情况下测试状态应该是红色的? (也许一些插件)

非常感谢任何提示!

即使有办法做到这一点,考虑到您执行的测试数量实际上可能会因合理原因从构建到构建减少这一事实,这听起来也不是个好主意。

首先我会尝试找出为什么这会意外发生。

有办法。

安装 Groovy Postbuild Plugin 并添加 Post 构建步骤

Post-Build-Actions -> 添加步骤 -> Groovy Postbuild

import jenkins.model.*

def currentBuild = manager.build
def totalCurrent 
def totalPrevious

// evaluate test count of current Build
def result = currentBuild .getAction(hudson.tasks.junit.TestResultAction.class).result
if (result != null) {
  totalCurrent = result.hasProperty( 'totalCount' ) ? result.totalCount : null
} 

// evaluate test count of previous Build
result = currentBuild .previousBuild.getAction(hudson.tasks.junit.TestResultAction.class).result
if (result != null) {
  totalPrevious = result.hasProperty( 'totalCount' ) ? result.totalCount : null
} 

// fail the build if test count reduced
if(totalCurrent < totalPrevious) {
  manager.buildFailure()
}

您可能需要添加更多 nullsafe 检查。