Test: error: 未识别的参数: —— cov = ner_brand —— cov-report = term-miss —— cov-config

当我试图通过命令行运行测试时

py.test  file_name.py

我得到了这个错误:

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

我该怎么补救?

69899 次浏览

如果要将 -cov 参数传递给 pytest,那么必须使用 pytest-cov 包 ,但默认情况下不应该传递该参数。您使用的是 py.test 的修改版本吗?

pip install pytest-cov

能解决你的问题。

对于那些使用 CentOS 6的人来说,setuptools的版本已经过时了,你还需要升级它:

pip install pytest-cov
pip install --upgrade setuptools

刚刚安装完 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

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

pipenv install pytest_cov

如果这里的其他答案对您不起作用,您可能已经在系统的其他地方安装了 py.test。在我的例子中,我在一个虚拟环境中遇到了这里描述的问题,但结果是 pytest 默认为我的系统安装(它没有安装 pytest-cov)。

停用您的虚拟环境或启动一个新的 shell 并运行以下命令确认:

pip3 freeze | grep pytest

(如果运行 python2,则为 pip freeze | grep pytest)

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

Turns out my versions mismatched.

我把版本改成了

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

然后就开始起作用了。

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

但是我当前的 python 设置(python --version)是 3.7.7.:

python -m pytest --version
pytest 6.2.1

类似地,您可以运行 python -m pytest file_name.py或者覆盖 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.)