当覆盖率太低时,如何使Jenkins stage失败并变红(使用C#和dotnet测试)?
我正在使用C#、coverlet.msbuild和Jenkins-Cobertura适配器。 我的詹金斯档案中大致有:当覆盖率太低时,如何使Jenkins stage失败并变红(使用C#和dotnet测试)?,c#,jenkins,cobertura,jenkins-blueocean,coverlet,C#,Jenkins,Cobertura,Jenkins Blueocean,Coverlet,我正在使用C#、coverlet.msbuild和Jenkins-Cobertura适配器。 我的詹金斯档案中大致有: stage ('Run unit tests') { steps { powershell "dotnet test -c:Release /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --no-build --no-restore --logger trx" } po
stage ('Run unit tests') {
steps {
powershell "dotnet test -c:Release /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --no-build --no-restore --logger trx"
}
post {
always {
step([$class: 'MSTestPublisher'])
publishCoverage failUnhealthy: true,
globalThresholds: [[thresholdTarget: 'Package', unhealthyThreshold: 50.0]],
adapters: [coberturaAdapter(
mergeToOneReport: true,
path: '**/*.cobertura.xml')]
}
}
}
如果包级别的覆盖率低于50%,这将导致我的Jenkins构建失败。到目前为止还不错
但是,当一个构建因此而失败时,它会对用户产生敌意,并且很难理解原因。“运行单元测试”阶段在蓝色海洋中是绿色的
当生成失败时,我是否可以使此阶段变为红色,以便更容易看到错误是什么?如果
publishCoverage
为true,则可以将currentBuild.result
设置为FAILURE
currentBuild.displayName
和currentBuild.description
可选:
post {
always {
script {
def failed = publishCoverage (failUnhealthy: true,
globalThresholds: [[thresholdTarget: 'Package', unhealthyThreshold: 50.0]],
adapters: [coberturaAdapter(
mergeToOneReport: true,
path: '**/*.cobertura.xml')])
if (failed) {
currentBuild.result = 'FAILURE'
currentBuild.displayName = "${currentBuild.displayName} Coverage"
currentBuild.description = "Coverage lower than 50%"
}
}
}
}
受Sers和我阅读的其他Jenkinsfile代码的启发,我找到了这个解决方案,它满足了我的需求:
stage ('Run unit tests') {
steps {
powershell "dotnet test -c:Release /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura --no-build --no-restore --logger trx"
}
post {
always {
step([$class: 'MSTestPublisher'])
publishCoverage failUnhealthy: true,
globalThresholds: [[thresholdTarget: 'Package', unhealthyThreshold: 50.0]],
adapters: [coberturaAdapter(
mergeToOneReport: true,
path: '**/*.cobertura.xml')]
script {
if (currentBuild.result == 'FAILURE') {
error("Test coverage is too low.")
}
}
}
}
}
这对我不起作用。我现在在我的詹金森档案里有一个和你非常相似的东西。即使工作失败,舞台仍然是绿色的。我的日志包含以下内容:“代码覆盖率强制失败:报告级别'Cobertura:/.Cobertura.xml'中的包覆盖率低于50.00健康阈值”。在日志中找不到Jenkins文件中的消息:(受您建议的启发,我尝试回显
publishCoverage
本身的返回值。结果表明publishCoverage
返回空值,无论FailUnhealth
是真是假。因此,if
块从未达到。有没有办法在不硬编码阈值的情况下实现它?也就是说,使bui失败ld仅当覆盖率在两个连续生成之间下降时?也许您可以以某种方式将当前覆盖率提取到一个变量,并在下一次运行时读回该变量。我不知道。添加以下内容将跟踪目标分支和块合并之间的覆盖率是否会下降-publishCoverage calculateDiffForChangeRequests:true,FailUnhealth:true,FailBuildIfCoverage DecreasedInChangeRequest:true,