"py.test"与"pytest"命令的区别

74

在我的情况下,py.test 命令失败了,而 pytest 运行得非常好。

我使用 pytest-flask 插件:

platform linux -- Python 3.5.2, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/sebastian/develop/py/flask-rest-template, inifile: 
plugins: flask-0.10.0

当我调用 $ py.test 时,我会得到以下错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 301, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('/home/sebastian/develop/py/flask-rest-template')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 332, in _importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('/home/sebastian/develop/py/flask-rest-template/conftest.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/_pytest/config.py", line 338, in _importconftest
    mod = conftestpath.pyimport()
  File "/usr/local/lib/python3.5/dist-packages/py/_path/local.py", line 650, in pyimport
    __import__(modname)
  File "/usr/local/lib/python3.5/dist-packages/_pytest/assertion/rewrite.py", line 207, in load_module
    py.builtin.exec_(co, mod.__dict__)
  File "/home/sebastian/develop/py/flask-rest-template/conftest.py", line 2, in <module>
    from app.app import create_app
  File "/home/sebastian/develop/py/flask-rest-template/app/app.py", line 1, in <module>
    from flask import Flask
ImportError: No module named 'flask'
ERROR: could not load /home/sebastian/develop/py/flask-rest-template/conftest.py

这是我的实际conftest.py文件:

import pytest
from app.app import create_app

@pytest.fixture
def app():
    app = create_app()
    return app

我的项目结构如下:

.
├── app
│   ├── __init__.py
│   ├── app.py
│   └── config.py   # flask configuration objects
├── conftest.py     # pytest configruation
├── requirements.txt
├── ...
└── tests
    └── ...

那么这两个命令的区别是什么?为什么一个失败了,而另一个没有?


更新1

1)我不得不将相对导入 from .config import Configfrom config import Config 更改为绝对导入,如 from app.config import Config

2)使用命令 python3 -m app.app 运行 Flask

3)现在 pytestpy.test 正常工作了

非常感谢你们的帮助!


更新2

这变得很奇怪了...当使用绝对导入、用-m选项运行 Python、以及开启 Flask 的debug=True时,werkzeug库没有像预期一样重新加载源代码:

http://chase-seibert.github.io/blog/2015/06/12/flask-werkzeug-reloader-python-dash-m.html

https://github.com/pallets/werkzeug/issues/461

https://github.com/pallets/flask/issues/1246

这在我的 app/app.py 中对我有所帮助:

if __name__ == '__main__':
    app.run(debug=True, use_reloader=False)

然后 python -m app.app 运行良好。


6
pytest 命令是一种新的方式,你可能还残留着旧版本的 pytest - wim
1
谢谢提示!所以以后我只会使用 pytest - swiesend
但是为了运行应用程序 python app/app.py,我需要 from config import Config。我该如何使两者都能正常工作(运行flask和pytest)? - swiesend
非常感谢大家的帮助!我明白了...所以我必须像这样始终运行它:python3 -m app.app,如https://dev59.com/2WQn5IYBdhLWcg3wP1JT中所述。因此,使用绝对导入,例如`from app.config import Config,那么py.test`也可以正常工作。 - swiesend
我使用 python3 -u -m pytest --color=auto,它非常好用。 - user1065000
显示剩余6条评论
2个回答

96
为了更详细地回答有关实际命令的问题(即在命令行上调用工具),我们来看一下:py.test 命令已经过时,pytest 是新的热门选择(从3.0版本开始)。py.testpytest 的命令将共存很长一段时间,但是有一天可能会弃用py.test, 所以我建议去掉 #dropthedot

[...] 从pytest 3.0开始,我们将支持和推荐使用pytest作为主要命令,而不是 py.test。 未来我们可能会弃用 py.test 并有可能移除它。

-- Dave Hunt

对于 pytest 社区来说向后兼容性是非常重要的问题,因此旧方法可能永远不会消失,在 setup.py 中将其定义为不同的入口点,维护负担也不大。

但至少显示一个警告会更有用,目前一些错误在使用pytest时不会发生,而使用py.test时却会发生,如果不了解py.test的弃用,则相当令人困惑。 - aderchox
1
@aderchox 我不知道这样的问题,也不确定它们如何表现不同(请查看我的回答中的 setup.py 链接 - 它实际上是完全相同的两个名称)。如果你真的在那里发现了一个问题,并且可以重现,请在 pytest 问题跟踪器上开启一个问题。 - Oliver Bestwalter

8

使用pytest ...或者更好的方式是python -m pytest ...

如果你还在某个地方看到旧名称,那显然是一个错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接