I have a problem where SonarQube is not able to parse the coverage reports , generated by pytest-cov. I have gone through StackOverflows posts and also the documentation on SQ, and have tried various options, but nothing works. Can someone tell me what is it that I am missing?
This is my project structure-
So, my root, contains a folder called "workflows", which in turn contains two subfolders , which are workflows and test. Now my source code is in ProjectRoot/workflows/workflows (highlighted in yellow in the screenshot of my project directory structure above) and my tests are in ProjectRoot/workflows/tests.
My .coveragec file, looks like
[run]
branch = True
source = workflows
relative_files = True
[report]
exclude_lines =
if self.debug:
pragma: no cover
raise NotImplementedError
if __name__ == .__main__.:
ignore_errors = True
omit =
tests/*
setup.py
# this file is autogenerated by dbx
workflows/common.py
skip_empty = True
Now when I run pytest in my build pipeline (Azure devops), I do it with a bash script like
cd $(Build.SourcesDirectory)/workflows
python3 -m pytest tests/unit --cov --cov-config=.coveragerc --cov-report xml --junitxml pytests.xml
This generates a coverage file like
<sources>
<source>workflows</source>
</sources>
<packages>
<package name="createexternaltables.tasks" line-rate="0" branch-rate="1" complexity="0">
<classes>
<class name="createtables.py" filename="createexternaltables/tasks/createtables.py" complexity="0" line-rate="0" branch-rate="1">
<methods/>
<lines>
<line number="2" hits="0"/>
<line number="4" hits="0"/>
<line number="7" hits="0"/>
<line number="10" hits="0"/>
<line number="12" hits="0"/>
<line number="13" hits="0"/>
<line number="14" hits="0"/>
<line number="19" hits="0"/>
<line number="20" hits="0"/>
<line number="22" hits="0"/>
<line number="24" hits="0"/>
<line number="25" hits="0"/>
<line number="31" hits="0"/>
<line number="32" hits="0"/>
</lines>
</class>
</classes>
</package>
In a subsequent step, I use SonarQube to analyze my source code (along with the coverage file generated by pytests
Here's how. If you notice I have set both the projectBaseDir and sources, since my tests and source code are inside subdirectories of the project root and not directly on the root
task: SonarQubePrepare@4
displayName: Prepare analysis on SonarQube
inputs:
SonarQube: SonarQube
scannerMode: CLI
configMode: manual
cliProjectKey: projectabc
cliProjectName: projectabc
cliProjectVersion: |
$(Build.DefinitionName) $(Build.SourceBranchName) $(GitBuildVersionSimple)
extraProperties: |
sonar.projectBaseDir=./workflows
sonar.language=py
sonar.python.version=3.9
sonar.python.xunit.reportPath=pytests.xml
sonar.python.coverage.reportPaths=workflows/coverage.xml
sonar.sources=workflows/
sonar.tests=tests/
I have tried with mutiple versions of the config, but no matter whatever, I do, I get the following error, when I do SonarQube Code Analyze.
Cannot resolve the file path 'createexternaltables/tasks/createtables.py' of the coverage report, the file does not exist in all 'source'.
What am I missing?
