SonarQube: Enterprise Edition Version 9.9
Deployed with Docker and integrated to GitLab pipeline.
Problem:
I run dotnet-sonarscanner and create python and dotnet coverage reports and test-reports in xml
The sonarqube shows all coverage reports but can’t find the classes of the pytest-results and write the warning:
WARN: The resource for ‘tests.test_XXX’ is not found, drilling down to the details of this test won’t be possible each test
Overview
I have a dotnet project with the following structure.
MyProject
MyProject/Main
MyProject/dotnet-lib1
MyProject/dotnet-lib2
MyProject/python-lib
and the pythest-lib structure is:
pytest-lib
pytest-lib/package1
pytest-lib/packgae2
pytest-lib/tests
The dotnet code is tested with nunit and the python part with pytest.
I created a pipeline for sonarqube in gitlab and I run a yaml script like this:
- cd MyProject
- dotnet sonarscanner begin
/k:"MyProject"
/d:sonar.login="${SONAR_TOKEN}"
/d:"sonar.host.url=${SONAR_HOST_URL}"
/d:sonar.cs.nunit.reportsPaths="test-result.xml"
/d:sonar.projectBaseDir=/builds/MyProject
/d:sonar.cs.vscoveragexml.reportsPaths="./coverage.xml"
/d:sonar.python.version=3.9
/d:sonar.python.coverage.reportPaths="./python-coverage.xml"
/d:sonar.python.xunit.reportPath="/builds/MyProject/python-lib/pytest-results.xml"
/d:sonar.exclusions="**/setup.py"
- dotnet restore
- dotnet build -c Release --no-incremental --no-restore
- dotnet-coverage collect 'dotnet test --logger:"nunit;LogFilePath=../test-result.xml"' -f xml -o './coverage.xml'
- cd ./python-lib
- python -m pytest --cov=. --cov-report=xml:../python-coverage.xml --junitxml=./pytest-results.xml
- cd ..
- dotnet sonarscanner end /d:sonar.login="${SONAR_TOKEN}"
So what must I change that the Processing report step of the python report will find the needed resources?
From the error message, it looks like the paths the scanner is seeing during analysis don’t match the paths it sees in the report. That’s the first thing to look at.
That said, I’m really not sure how much “drilling down to details of [a] test” is possible.
Hi Ann, thank you for reply,
and yes, I am with you, I think there is a path problem, but I don’t know how to fix it.
From my point of view the problem is, the sonar scanner is running from project root and the pytest is running from python-lib folder. And when the sonar scanner tries to analyze the test report it cannot resolve the path of the python parts.
But when I run the pytest from another location with a path attribute the result in the report is the same.
I hope someone knows an additional param which I must set for the sonar scanner to fix this or see anything which I maybe configured wrong.
You’re going to need to fix this on the report side. You say that when you run pytest from project root, you get the same paths in the report as when you run it from the Python module? If so, you’ll probably need to do a step in the middle to ‘correct’ the pathing in the report.
INFO: Sensor PythonXUnitSensor [python]
INFO: Processing report './pytest-results.xml'
WARN: The resource for 'python_lib.tests.test_main' is not found, drilling down to the details of this test won't be possible
WARN: The resource for 'python_lib.tests.test_other' is not found, drilling down to the details of this test won't be possible
INFO: Sensor PythonXUnitSensor [python] (done) | time=116ms
What I now know is that the pytest.ini must also be in the root folder, which I previously had in the python_lib folder. Because the pytest.ini set with his own location the root folder for the pytest.
Since then, the results of the pytest are also provided with the correct path from my point of view. Because before I moved the ini the path for each test in the python-result.xml was: “tests.test_MyModule1”
and now it is “python_lib.tests.test_MyModule1”.
So if this is not the correct path from the sonar scanner’s point of view, what is the expected path?