1

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%"
            }
        }

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but when I use coverage report --fail-under=80 in my Jenkins pipeline it reports class org.codehaus.groovy.ast.expr.UnaryMinusExpression, with its value 'under', is a bad expression as the left hand side of an assignment operator at line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.