Coverage fails in tox file

We use SonarCloud - Github with Github Actions.
Our repository : Python , pytest

For getting test coverage properly I used the build.yml as suggested here. I only edited the tox.ini file and .properties file. But I have problem for tox file:

[tox]
envlist = py39
skipsdist = True
 
[testenv]
deps =
    pytest
    coverage
commands = pytest --cov={my project name} --cov-report=xml --cov-config=tox.ini --cov-branch
 
[coverage:run]

However, when it is executed on Github Actions, I get the error below:

py: install_deps> python -I -m pip install coverage pytest

py: freeze> python -m pip freeze --all

py:attrs==22.2.0,coverage==7.0.4,exceptiongroup==1.1.0,iniconfig==2.0.0,packaging==23.0,pip==22.3.1,pluggy==1.0.0,pytest==7.2.0,setuptools==65.6.3,tomli==2.0.1,wheel==0.38.4

py: commands[0]> pytest --cov={my project name} --cov-report=xml --cov-config=tox.ini --cov-branch

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] […]

pytest: error: unrecognized arguments: --cov={my project name} --cov-report=xml --cov-config=tox.ini --cov-branch

inifile: None

rootdir: /home/runner/work/{my project name}/{my project name}

py: exit 4 (0.19 seconds) /home/runner/work/{my project name}/{my project name}> pytest --cov={my project name} --cov-report=xml --cov-config=tox.ini --cov-branch pid=1977

py: FAIL code 4 (3.40=setup[3.21]+cmd[0.19] seconds)

evaluation failed :frowning: (3.46 seconds)

Error: Process completed with exit code 4.

  1. How can I solve this issue?
  2. What is the correct way to write project name? {organisation name}-{project name}?
  3. What should be written for pytest attributes?:
  • –cov=
  • –cov-report=
  • –cov-config=
  • –cov-branch=

Hi @Furkan_YY,

Thanks for reporting this, we created a ticket for this and we’ll update this page soon.

Hi @Furkan_YY ,
I think there is indeed a mistake in the documentation.
The tox file should declare pytest-cov as dependency in order to be able to use --cov related arguments.

[tox]
envlist = py39
skipsdist = True
 
[testenv]
deps =
    pytest
    pytest-cov # <---- changed here
commands = pytest --cov={my project name} --cov-report=xml --cov-config=tox.ini --cov-branch
 
[coverage:run]

What should be written for pytest attributes?

--cov should refer to the entry point package of your python project.
--cov-report should be xml
--cov-config should point to the tox file
--cov-branch : no value needed, just need to be enabled.

You can have a look at this example: https://github.com/andreaguarino/python-demo-project/blob/main/tox.ini

Hope it helps,
Andrea G.

2 Likes

Hello @Andrea_Guarino ,

We edited the files according to your reply.

sonar-project.properties file:

...
sonar.sources=cerberus
sonar.tests=tests
...

tox.ini file:

[tox]
envlist = py39
skipsdist = True

[testenv]
deps =
    pytest
    pytest-cov
commands = pytest --cov=cerberus --cov-report=xml --cov-config=tox.ini --cov-branch

[coverage:run]

However, we are getting zero coverage and have the warning below. We don’t understand what the issue is.

ps. if we go to private chat, i can provide more details if you need.


Can you please help us? @tony.briget @Andrea_Guarino

Hello,
Can anyone help us with this? @tony.briget @Andrea_Guarino

Hello @ganncamp @Colin ,
Sorry for including you in this ticket, but I really need support. Can anyone please help?

Hey there.

This is a community forum, not a ticketing system. Sometimes, it takes time for other users/SonarSourcers to come back to you. I welcome you to read our FAQ.

1 Like

Hi @Furkan_YY ,

Did you add the relative_files = True under [coverage:run] section?
From the error message it looks like <source> is still using absolute paths, i.e. /home/runner/work/...

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.