I am using C#, coverlet.msbuild and the Jenkins Cobertura adapter. I have roughly this in my 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')]
}
}
}
This makes my Jenkins build fail if coverage is below 50% at package level. So far so good.
But when a build fails due to this, it is user-hostile and hard to see why. The 'Run unit tests' stage is green in Blue Ocean.
Can I make this stage turn red when it fails the build, so it is easier to see what the error is?
Inspired by the answer from Sers and some other Jenkinsfile code I read through, I arrived at this solution, which does what I want:
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.")
}
}
}
}
}