Coverage & Python/Django No coverage information - problems with paths

Template for a good new topic, formatted with Markdown:

  • ALM used: GitHub
  • CI system used: Github Actions
  • Scanner command used: SonarSource/sonarcloud-github-action@master

Command launch from Python:

pipenv run coverage run -m pytest --disable-pytest-warnings -n 4 --cov-report=xml
  • Error observed (wrap logs/code around with triple quotes ``` for proper formatting):
INFO: Parsing report '/github/workspace/./api_correcto/coverage.xml'
WARN: Invalid directory path in 'source' element: /home/runner/work/api.backend/api.backend/api_correcto/api_correcto
ERROR: Cannot resolve the file path '__init__.py' of the coverage report, the file does not exist in all 'source'.
ERROR: Cannot resolve 78 file paths, ignoring coverage measures for those files

INFO: Processing report './api_correcto/xunit.xml'
WARN: The resource for 'api_correcto.apps.checkouts.tests.test_controllers.CheckoutControllersTestCase' is not found, drilling down to the details of this test won't be possible
WARN: The resource for 'api_correcto.apps.checkouts.tests.test_services.CheckoutServicesTestCase' is not found, drilling down to the details of this test won't be possible
WARN: The resource for 'api_correcto.apps.checkouts.tests.test_controllers.CheckoutControllersTestCase' is not found, drilling down to the details of this test won't be possible
WARN: The resource for 'api_correcto.apps.checkouts.tests.test_services.CheckoutServicesTestCase' is not found, drilling down to the details of this test won't be possible
WARN: The resource for 'api_correcto.apps.checkouts.tests.test_services.CheckoutServicesTestCase' is not found, drilling down to the details of this test won't be possible
WARN: The resource for 'api_correcto.apps.auth.tests.tests.rest.v1_0.refresh_token.test_contract_refresh_token.ContractRefreshTokenAPITests' is not found, drilling down to the details of this test won't be possible

coverage.xml

<?xml version="1.0" ?>
<coverage version="7.3.2" timestamp="1698768932115" lines-valid="761" lines-covered="740" line-rate="0.9724" branches-covered="0" branches-valid="0" branch-rate="0" complexity="0">
	<!-- Generated by coverage.py: https://coverage.readthedocs.io/en/7.3.2 -->
	<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->
	<sources>
		<source>/home/runner/work/api.backend/api.backend/api_correcto/api_correcto</source>
	</sources>
	<packages>
		<package name="." line-rate="1" branch-rate="0" complexity="0">
			<classes>
				<class name="__init__.py" filename="__init__.py" complexity="0" line-rate="1" branch-rate="0">
					<methods/>
					<lines/>
				</class>
				<class name="urls.py" filename="urls.py" complexity="0" line-rate="1" branch-rate="0">
					<methods/>
					<lines>
						<line number="1" hits="1"/>
						<line number="2" hits="1"/>
						<line number="3" hits="1"/>
						<line number="5" hits="1"/>
						<line number="6" hits="1"/>
						<line number="7" hits="1"/>
						<line number="9" hits="1"/>
					</lines>
				</class>
			</classes>
		</package>

xunit.xml

<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="188" time="243.259" timestamp="2023-10-31T16:11:30.273631" hostname="fv-az589-852"><testcase classname="api_correcto.apps.auth.tests.tests.rest.v1_0.sign_in.test_sign_in.SignInAPITests" name="test_sign_in_wrong_type_password" time="137.290"><system-out>--------------------------------- Captured Out ---------------------------------

</system-out></testcase><testcase classname="api_correcto.apps.checkouts.tests.test_services.CheckoutServicesTestCase" name="test_create_new_checkout" time="137.800"><system-out>--------------------------------- Captured Out ---------------------------------

</system-out></testcase><testcase classname="api_correcto.apps.auth.tests.tests.rest.v1_0.sign_in.test_sign_in.SignInAPITests" name="test_sign_in_wrong_type_username" time="2.227"><system-out>--------------------------------- Captured Out ---------------------------------

tree directory in Github Action

api.backend
    └── api.backend
        ├── Dockerfile
        ├── Pipfile
        ├── README.md
        ├── api_correcto
        │   ├── api_correcto
        │   │   ├── __init__.py
        │   │   ├── api_core
[...]
        │   │   ├── asgi.py
        │   │   ├── settings
        │   │   │   ├── __init__.py
        │   │   │   ├── base.py
        │   │   │   ├── ci.py
        │   │   │   ├── local.py
        │   │   │   └── prod.py
        │   │   ├── test_utils
        │   │   │   ├── __init__.py
        │   │   │   └── mixins.py
        │   │   ├── urls.py
        │   │   └── wsgi.py
        │   ├── coverage.xml
        │   ├── manage.py
        │   ├── pytest.ini
        │   └── xunit.xml
        ├── docker-compose.yml
        ├── languagetool
        │   └── data
        │       └── dump.sql
        └── sonar-project.properties

Why isn’t Sonar able to retrieve coverage information when files exist and paths are correct? Could you help me to solve this issue with Python?

Hi,

The paths in the coverage report need to match the paths analysis sees.

It’s not clear to me where you’re launching analysis from, but

I don’t understand the /./ in this path. Did you provide ./api_correcto/coverage.xml as the report path? If so, there’s no need to prefix it with ./

This already contradicts the report path we have in the analysis log. So far, it seems that the report and the source files are in entirely different paths from root…?

And this tree makes me wonder seriously about where analysis is taking place from. Because it too is vastly different from the report path, which was /github/workspace/./api_correcto/coverage.xml

 
Ann

Hi Ann,

First of all, thank you so much for your answer and support. Truly appreciate it. We want to be able to solve this for our project. It causing some problems.

The analysis is launched from Github Actions:

      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

This is the configuration for Sonar:

sonar.projectKey=correcto-com_api.backend
sonar.organization=correcto-com
sonar.coverage.exclusions=**/settings/**, **/tests/**, **/migrations/**, **/wsgi.py, **/asgi.py, **/fixtures/**, **/migrations/**
sonar.exclusions=**/settings/**, **/migrations/**, **/wsgi.py, **/asgi.py, **/fixtures/**, **/migrations/**

sonar.python.coverage.reportPaths=./api_correcto/coverage.xml
sonar.python.xunit.reportPath=./api_correcto/xunit.xml
sonar.projectBaseDir=./api_correcto/api_correcto/

# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
sonar.sources=.

Perhaps, this could be helpful in providing more clarity to your questions. How can I assist you better?

Hi,

In GitHub, what is the file system path from project root to the coverage report?

 
Ann

Hi Ann,

Let me try to answer your question.

In Github,

/home/runner/work/api.backend/api.backend/api_correcto/

/home/runner/work/
└── api.backend
    └── api.backend
        ├── Dockerfile
        ├── Pipfile
        ├── README.md
        ├── api_correcto
        │   ├── api_correcto
        │   │   ├── __init__.py
      [....]  
        │   │   ├── asgi.py
        │   │   ├── settings
        │   │   │   ├── __init__.py
        │   │   │   ├── base.py
        │   │   │   ├── ci.py
        │   │   │   ├── local.py
        │   │   │   └── prod.py
        │   │   ├── test_utils
        │   │   │   ├── __init__.py
        │   │   │   └── mixins.py
        │   │   ├── urls.py
        │   │   └── wsgi.py
        │   ├── coverage.xml
        │   ├── manage.py
        │   ├── pytest.ini
        │   └── xunit.xml
        ├── docker-compose.yml
        ├── ruff.toml
        ├── sonar-project.properties
        ├── template.env
        └── tox.ini

Hi,

Maybe I would understand if I had a deeper background in GH Actions, but I don’t. Where in that^ file path does analysis start from?

 
Ann

Hi Ann,

How could that affect the coverage results? it just another Ubuntu container. It would depend on how Sonar manages the report directly since all the paths seem correct.

Maybe it is related with that issue you have opened: [SONARPY-1203] - Jira

Hi,

How could that affect the coverage results? Well your coverage results aren’t being read:

As I stated in my initial response,

And

At any rate, you need to sort out your pathing. Make sure the paths in the coverage report match the paths to the files that analysis sees.

 
HTH,
Ann

Sorry Ann but imho It seems depend on how Sonar manages the report directly since all the paths are correct.

Is there no official support available for Sonar issues? I mean, it’s not just an open source project. There’s a paid version too. When you face problems with any other product, you get official support to try and solve it.

Hi,

Professional support is available.

 
Ann

A few weeks ago, I reached out for official and professional support, but was redirected to this forum/community. However, I am still unsure about how to properly manage the issue.

I believe I require the assistance of someone from Sonar’s product engineering team.

Hi,

Maybe this will help:

 
Ann