首页 > 解决方案 > pytest 或 py.test,以及不一致的文档

问题描述

我决定试一试。

Virtualenv 已启动(python 3.5.2) pip install -U pytest (根据:https ://docs.pytest.org/en/latest/getting-started.html )。

下一个文档说我应该运行:

$ pytest --version
This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py

但这实际上是我得到的:

$ pytest --version
Usage: pytest [OPTIONS] [testfile [testpattern]]

examples:

pytest path/to/mytests.py
pytest path/to/mytests.py TheseTests
pytest path/to/mytests.py TheseTests.test_thisone
pytest path/to/mytests.py -m '(not long and database) or regr'

pytest one (will run both test_thisone and test_thatone)
pytest path/to/mytests.py -s not (will skip test_notthisone)


pytest: error: no such option: --version

但是如果我运行:

py.test --version
This is pytest version 3.9.1, imported from ${HOME}/${V_ENV}/lib/python3.5/site-packages/pytest.py

根据那个答案:

“py.test”与“pytest”命令

py.test 是旧的(deprecated他们说)和 pytest 是要走的路。

我检查了他们两个:

$ which pytest
$HOME/$V_ENV/bin/pytest

$ which py.test
$HOME/$V_ENV/bin/py.test

完全相同的文件。

$HOME 是我的家,但 $V_ENV 是我保存virutalenvs 的地方(我使用 virtualenvwrapper)。

运行测试时:

有用:

$ py.test tests/

它没有(例外):

$ pytest tests/

我检查了堆栈跟踪。

py.test 使用 python3 运行(正确) pytest 使用 python2 运行(不正确,它来自 os)

有谁知道发生了什么?

看起来 py.test 和 pytest 完全一样,所以不使用 py.test 的概念似乎有点过时了。我这样说对吗?

标签: pythonpytest

解决方案


碰巧 py 安装在系统 python 库中(可能是另一个依赖项)。有一种一致的方法可以重现该错误。它涉及以下步骤。

  1. 创建虚拟环境
  2. 运行 pytest --version (它会抱怨 pytest 没有 --version)
  3. 点安装 pytest
  4. 运行 pytest --version (它会抱怨 pytest 没有 --version)

它将保持这种状态。但是,如果您停用并激活 env 或完全关闭 shell 并重新打开它,它将修复它。

=== 老安维尔 ===

感谢@hoefling,他给了我正确的方向!

有一个名为 py 的旧库作为 tox 的依赖项安装。看起来在与安装和/或运行这两个库相关的某些情况下,您最终可能会损坏安装。最初我认为这是完全的安装顺序(先安装 tox,然后再安装 pytest),但我无法重现。所以目前的工作理论是我首先运行 tox 导致它创建无效的 virtualenv,然后我安装了损坏的 PYTHONPATH 或 PATH 的 pytest。

简而言之。有一种方法可以用毒害你的 pytest,如果它发生从头开始人们!


推荐阅读