【问题标题】:Is Python 3.9.6 compatible with pytest 6.2.5?Python 3.9.6 是否与 pytest 6.2.5 兼容?
【发布时间】:2022-01-23 01:59:17
【问题描述】:

我正在尝试测试用 pycharm 编写的 Django REST API 的视图和模型,并为此安装了 pytest。我已经编写了一些测试,当我想启动它们时,我收到了以下错误消息:

ERROR: usage: _jb_pytest_runner.py [options] [file_or_dir] [file_or_dir] [...]
_jb_pytest_runner.py: error: unrecognized arguments: --cov=frontend --cov-report=html

然后我检查了我是否正确安装了 pytest,看来我已经正确安装了。我同时安装了 Python 2.7.16 和 Python 3.9.6,但使用的是 Python 3。这可能是兼容性问题还是其他原因?

我尝试通过终端使用 py.test 和仅在 IDE 本身中启动测试。我不断收到同样的错误消息。

我尝试了以下方法:

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

但我似乎遇到了同样的错误。

ERROR: usage: _jb_pytest_runner.py [options] [file_or_dir] [file_or_dir] [...]
_jb_pytest_runner.py: error: unrecognized arguments: --cov=frontend --cov-report=html

有谁知道我该如何解决这个问题?

提前致谢。

【问题讨论】:

标签: python django rest pytest pytest-django


【解决方案1】:

首先,是的,Python 3.9.6 与 pytest 6.2.5 兼容,但是,您似乎缺少一些依赖项。 pytest 是许多不同的 Python 包之一,您似乎已经成功安装了它,所以您已经完成了一半。

有几个不同的覆盖插件可以与 pytest 一起使用,这些插件需要单独安装。以下是 Python 和 pytest 最常见的两个覆盖率插件:

https://pypi.org/project/coverage/

https://pypi.org/project/pytest-cov/

第一个,coverage 安装有:

pip install coverage

第二个,pytest-cov 安装有:

pip install pytest-cov

根据您的运行命令,您似乎想要使用pytest-cov。安装后,您可以通过调用 pytest --help 来验证 pytest 是否具有这些新选项:

> pytest --help

...
coverage reporting with distributed testing support:
  --cov=[SOURCE]        Path or package name to measure during execution (multi-allowed). Use --cov= to
                        not do any source filtering and record everything.
  --cov-reset           Reset cov sources accumulated in options so far.
  --cov-report=TYPE     Type of report to generate: term, term-missing, annotate, html, xml (multi-
                        allowed). term, term-missing may be followed by ":skip-covered". annotate, html
                        and xml may be followed by ":DEST" where DEST specifies the output location.
                        Use --cov-report= to not generate any output.
  --cov-config=PATH     Config file for coverage. Default: .coveragerc
  --no-cov-on-fail      Do not report coverage if test run fails. Default: False
  --no-cov              Disable coverage report completely (useful for debuggers). Default: False
  --cov-fail-under=MIN  Fail if the total coverage is less than MIN.
...

或者,您也可以使用 coverage 获得相同的结果:

coverage run -m pytest

coverage html

coverage report

即使不使用pytest-cov 选项,这也会为您提供覆盖率报告。

【讨论】:

  • 这似乎有效。看来我可以使用 coverage run -m pytest 来运行测试。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 2016-01-02
  • 2017-06-07
  • 2011-05-23
  • 2011-10-22
相关资源
最近更新 更多