【发布时间】:2018-10-18 14:46:18
【问题描述】:
我决定试一试。
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.test是旧名称,其中pytest是名为py的大型包的一部分(不再开发)。一旦他们转移到一个单独的包,他们将名称更改为pytest。命令py.test已弃用,最终将被删除。我猜你的问题是由于pytest的错误安装造成的,大概是因为你在虚拟环境中安装时使用了--user。创建一个新的虚拟环境并从头开始重新安装:/path/to/venv/bin/pip install pytest,问题是否仍然出现? -
这不是 --user 开关,但您的建议是正确的。我从头开始,发现它正在工作。下一个罪魁祸首是已安装库的列表,我检查了它们,发现你提到的
py。我检查了它作为依赖项安装的内容,它是一个名为 tox 的库。建议与 pytest 本身配合得很好的图书馆,互联网上说 go pytest with tox。这将节省您的时间和精力。所以现在是另一个练习,如何处理 tox 以便它不会安装 py。 :-D 干杯!