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: python-demo-project/tox.ini at main · andreaguarino/python-demo-project · GitHub

Hope it helps,
Andrea G.

2 Likes