jenkinssonarqubepipenvcoverage.pytest-coverage

Sonar can't read coverage xml report for python application


I'm trying to analyse a python application in which I'm using pipenv to manage dependencies. The Jenkinsfile is as the following

        stage('Install dependencies') {
            steps {
                    echo('....Install dependencies & Create VirtualEnvironment ..')
                    sh 'pip3 install --user pipenv'
                    sh 'pipenv --rm || exit 0'
                    sh 'pipenv install --pre --dev'
            }
        }
        stage('Unit & Integration tests') {
            steps {
                echo('.... Unit & Integration tests')
                sh "pipenv run coverage run --source=application/src/jobs -m pytest -v --junit-xml=reports/report.xml  ."
                sh "pipenv run coverage report"

                stash includes: '*', name: 'workspace'
                stash includes: 'reports/*' , name: 'reports'
            }
        }
        stage('Build & SonarQube analyses') {
            agent { label 'node14' }

            steps {
                echo('Build & SonarQube analyses')
                unstash 'reports'
                sh '/opt/sonar-scanner/bin/sonar-scanner \
                -Dsonar.projectKey=$sonarProjectKey \
                -Dsonar.sources=./application/src/ \
                -Dsonar.host.url=$sonarHost \
                -Dsonar.login=$sonarApiKey \
                -Dsonar.python.coverage.reportPaths=./reports/report.xml \
                -X '
            }
        }

The coverage report is generated successfully and I can visualize it by running coverage report But Sonar isn't able to update the coverage

Any suggestion please


Solution

  • The problem was in the xml report format, I used converage xml -i to generate the report and it works

    stage('Unit & Integration tests') {
                steps {
                    echo('.... Unit & Integration tests')
                    sh "pipenv run coverage run --source=application/src/jobs -m pytest -v --junit-xml=reports/report.xml  ."
                    sh "pipenv run coverage report"
                    sh "pipenv run coverage xml -i"  // generate the report that will be parsed by Sonar
    
                    stash includes: 'coverage.xml' , name: 'coverage'
                }
            }
    
            stage('Build & SonarQube analyses') {
                agent { label 'node14' }
    
                steps {
                    echo('Build & SonarQube analyses')
                    unstash 'coverage'
                    sh '/opt/sonar-scanner/bin/sonar-scanner \
                    -Dsonar.projectKey=$sonarProjectKey \
                    -Dsonar.sources=./application/src/ \
                    -Dsonar.host.url=$sonarHost \
                    -Dsonar.login=$sonarApiKey \
                    -Dsonar.python.coverage.reportPaths=coverage.xml \
                    -X '
                }
            }