Python test count not possible

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?

Hi,

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.

 
HTH,
Ann

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.

Hi,

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.

 
HTH,
Ann

Ok I moved my pytest.ini from the python_lib folder to the MyProject folder and execute the pytest also from this folder.

Now my report has from my point of view the correct path.

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
    <testsuite hostname="myhost" timestamp="2023-11-15T09:43:21.946237" time="1.012" tests="2" skipped="0" failures="0" errors="0" name="pytest">
        <testcase time="0.032" name="test_main" classname="python_lib.tests.test_main"/>
        <testcase time="0.024" name="test_other_stuff" classname="python_lib.tests.test_other"/>
    </testsuite>
</testsuites>

But the sonar scanner is still warning:

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

Hi,

I’m not sure what to tell you. You may need an interim process to ‘correct’ the paths.

 
HTH,
Ann

Yes but what is the path the sonar scanner expect? Again my structure:

MyProject (root folder)
-- DotnetLib1
-- DotnetLib2
-- python_lib
   -- tests
       - test_MyModule1.py
       - test_MyModule2.py 
   -- modules
       - MyModule1.py
       - MyModule2.py

From root folder I do:

  • Start Sonar scanner
  • run “dotnet build”
  • run “dotnet test”
  • run “pytest”
  • Stop Sonar scanner

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?

The warning “WARN: The resource for ‘tests.test_XXX’ is not found, drilling down to the details of this test won’t be possible” in SonarQube when processing Python test reports usually indicates a mismatch between the paths or identifiers used in your test reports and what SonarQube expects.

Here are several steps and considerations to troubleshoot and potentially resolve this issue:

1. Check Test Report Paths

  • Ensure that the path specified for sonar.python.xunit.reportPath is correct and accessible from the SonarQube scanner context. In your script, this is set to /builds/MyProject/python-lib/pytest-results.xml.
  • Make sure that the pytest-results.xml file is being generated at the expected location.

2. Verify Report Content

  • Open pytest-results.xml and verify that the class names and package structure match what SonarQube expects. SonarQube needs to be able to map the class names in the test report to the actual source files.
  • The class names in the test report should correspond to the file paths of your tests. If there’s a discrepancy, SonarQube will not be able to link the test results to the files.

3. Review Project Structure

  • SonarQube’s ability to find the test classes might be affected by how your project and its directories are structured.
  • Make sure that the structure of your Python tests in your repository matches the structure in the test report.