python - 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 是旧的(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 的概念似乎有点过时了。我这样说对吗?
解决方案
碰巧 py 安装在系统 python 库中(可能是另一个依赖项)。有一种一致的方法可以重现该错误。它涉及以下步骤。
- 创建虚拟环境
- 运行 pytest --version (它会抱怨 pytest 没有 --version)
- 点安装 pytest
- 运行 pytest --version (它会抱怨 pytest 没有 --version)
它将保持这种状态。但是,如果您停用并激活 env 或完全关闭 shell 并重新打开它,它将修复它。
=== 老安维尔 ===
感谢@hoefling,他给了我正确的方向!
有一个名为 py 的旧库作为 tox 的依赖项安装。看起来在与安装和/或运行这两个库相关的某些情况下,您最终可能会损坏安装。最初我认为这是完全的安装顺序(先安装 tox,然后再安装 pytest),但我无法重现。所以目前的工作理论是我首先运行 tox 导致它创建无效的 virtualenv,然后我安装了损坏的 PYTHONPATH 或 PATH 的 pytest。
简而言之。有一种方法可以用毒害你的 pytest,如果它发生从头开始人们!
推荐阅读
- rust - 从base58字符串转换为十六进制字符串的算法
- android - 类型不匹配。所需上下文:找到 MyAdapter.ViewHolder
- android - 使用 FirestorePagingAdapter 时手动插入项目
- c# - 如何让玩家从一个 x 位置移动到另一个位置?
- wordpress - Wordpress 在元数据中显示来自多个值的帖子
- git - 在不与 master 合并的情况下测试新创建的分支 || 混帐
- r - R:使用索引向量访问二维矩阵的元素
- android-fragments - 片段的 onCreateOptionsMenu() 等价物是什么?
- node.js - Mongoose 模式:如何设置数组中的最大项目数?
- django - 将 help_text 添加到 UserCreationForm