py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config

PythonPytest

Python Problem Overview


when I am trying to run my test through command line

py.test  file_name.py

I got this error:

py.test: error: unrecognized arguments: --cov=ner_brands --cov-report=term-missing --cov-config

How can I fix this?

Python Solutions


Solution 1 - Python

pytest-cov package is required if you want to pass --cov arguments to pytest, by default it should not be passed though. Are you using a modified version of py.test?

pip install pytest-cov

would fix your issue.

Solution 2 - Python

For those who use CentOS 6, the version of setuptools is old and you need to upgrade it also:

pip install pytest-cov
pip install --upgrade setuptools

Just after installing pip install pytest-cov:

~ # py.test --version
This is pytest version 3.0.5, imported from /usr/lib/python2.6/site-packages/pytest.pyc

~ # pip install --upgrade setuptools
[...]
Successfully installed setuptools-30.3.0

~ # py.test --version
This is pytest version 3.0.5, imported from /usr/lib/python2.6/site-packages/pytest.pyc
setuptools registered plugins:
  pytest-cov-2.4.0 at /usr/lib/python2.6/site-packages/pytest_cov/plugin.py

Solution 3 - Python

If the other answers here didn't work for you, you may have py.test installed somewhere else in your system. In my case, I ran into the issue described here inside a virtual environment, but it turned out that pytest was defaulting to my system installation (which did not have pytest-cov installed).

Deactivate your virtual environment or start a new shell and run the following to confirm:

pip3 freeze | grep pytest

(or pip freeze | grep pytest if you're running python2)

If you find it, try uninstalling it, then reactivate your virtual environment and try again.

Solution 4 - Python

sdonk's answer helped me. But since I use pipenv, I had to run

pipenv install pytest_cov

Solution 5 - Python

Turns out my versions mismatched.

I changed the versions to

pytest="*"
pytest-cov="*"

and it started to work.

Solution 6 - Python

On my Ubuntu, I had also similar issue which was caused by having wrong binary for pytest:

py.test --version
This is pytest version 4.6.11, imported from /home/myhome/.local/lib/python2.7/site-packages/pytest.pyc

But my current python setup (python --version) was 3.7.7.. I had to run this instead:

python -m pytest --version
pytest 6.2.1

Similarly you can run python -m pytest file_name.py or for coverage python -m pytest --cov=my_project tests/.

I always recommend to check this especially when there are any issues and I think it's a good practice to run this with -m instead of using pytest directly as it may easily happen it points to different version than the one that should be used within your current python environment. (See similar explanation here.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHello ladView Question on Stackoverflow
Solution 1 - PythonsdonkView Answer on Stackoverflow
Solution 2 - PythonSamuel PhanView Answer on Stackoverflow
Solution 3 - PythondaveruinseverythingView Answer on Stackoverflow
Solution 4 - PythonBrontesView Answer on Stackoverflow
Solution 5 - PythonSouradeep NandaView Answer on Stackoverflow
Solution 6 - PythonNerxisView Answer on Stackoverflow