py.test:错误:无法识别的参数:--cov=ner_brands --cov-report=term-missing --cov-config

问题描述 投票:0回答:6

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

py.test  file_name.py

我收到此错误:

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

我该如何解决这个问题?

python pytest
6个回答
269
投票
如果您想将

--cov 参数传递给 pytest,则需要

pytest-cov 包
。 但默认情况下它不会通过。您使用的是
py.test
的修改版本吗?

pip install pytest-cov

可以解决您的问题。


14
投票

对于使用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

9
投票

如果这里的其他答案不适合您,您可能已将 py.test 安装在系统中的其他位置。就我而言,我在虚拟环境中遇到了此处描述的问题,但事实证明 pytest 默认为我的系统安装(未安装 pytest-cov)。

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

pip3 freeze | grep pytest

(或者

pip freeze | grep pytest
如果你运行的是 python2)

如果找到它,请尝试卸载它,然后重新激活虚拟环境并重试。


5
投票

sdonk的回答对我有帮助。但由于我使用pipenv,所以我必须运行

pipenv install pytest_cov

4
投票

在我的 Ubuntu 上,我也遇到了类似的问题,这是由于

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/

我总是建议检查这一点,特别是当出现任何问题时,我认为使用

-m
运行它而不是直接使用
pytest
是一种很好的做法,因为它很容易发生,它指向与应该的版本不同的版本在当前的 python 环境中使用。 (参见类似的解释这里。)


2
投票

结果我的版本不匹配。

我将版本更改为

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

它开始起作用了。

© www.soinside.com 2019 - 2024. All rights reserved.