I came up with the below piece of code(jenkins pipeline) to run UTs and generate the coverage report for my Python application. Now, I'm looking to fail the build if code coverage is lesser than 80% but not sure if there is a readymade library to validate the coverage report for Python, like Jacoco or Cobertura for Java projects. Or Is my understanding incorrect ?
def call(Map params = [:]) {
dir(params.testDir) {
if (params.env == 'python') {
sh "python -m pytest --verbose --cov=${params.testDir} --cov-report=xml:coverage.xml --cov-report html:coverage --junit-xml=unittest.xml test"
junit testResults: "*.xml", skipPublishingChecks: true
}
}
}
I saw this code snippet but whether coberturaAdapter works in my case, as I'm using pytest ?
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%"
}
}
You can add a pytest argument: --cov-fail-under=MIN
to fail the build.
Alternatively, you can also add a follow-up command after the execution of pytest:
coverage report --fail-under=MIN
Read more at: https://pytest-cov.readthedocs.io/en/latest/config.html#reference and https://coverage.readthedocs.io/en/6.3.2/cmd.html#coverage-summary-coverage-report