"py.test" 与 "pytest" 命令
"py.test" vs "pytest" command
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 Config
或 from config import Config
更改为绝对导入,例如 from app.config import Config
2) 运行 烧瓶 python3 -m app.app
3) 现在 pytest
和 py.test
工作正常
非常感谢大家的帮助!
更新 2
这变得很奇怪...当使用绝对导入时,运行 python 带有 -m
选项,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
工作正常。
使用 pytest ...
甚至更好 python -m pytest ...
.
你可以忘记旧名称,如果你仍然在某个地方发现它,那显然是一个错误。
更详细地回答有关实际命令(意味着在命令行上调用该工具)的问题:
py.test
调用是旧的和失败的联合。 pytest
是新热度(自 3.0 起)。我猜 py.test
和 pytest
调用将共存很长时间,但在某些时候 py.test
可能会被弃用。所以我会推荐给#dropthedot。
[...] so from pytest 3.0 we will supported and recommend use of pytest as the main command instead of py.test. It's possible that in future we will deprecate py.test and potentially even remove it.
-- Dave Hunt
向后兼容性是 pytest 社区的一个非常重要的问题,因此旧方法可能永远不会消失,并且无论如何保留它都不会造成太大的维护负担(它只是在 [= 中被定义为不同的入口点) 18=]).
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 Config
或 from config import Config
更改为绝对导入,例如 from app.config import Config
2) 运行 烧瓶 python3 -m app.app
3) 现在 pytest
和 py.test
工作正常
非常感谢大家的帮助!
更新 2
这变得很奇怪...当使用绝对导入时,运行 python 带有 -m
选项,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
工作正常。
使用 pytest ...
甚至更好 python -m pytest ...
.
你可以忘记旧名称,如果你仍然在某个地方发现它,那显然是一个错误。
更详细地回答有关实际命令(意味着在命令行上调用该工具)的问题:
py.test
调用是旧的和失败的联合。 pytest
是新热度(自 3.0 起)。我猜 py.test
和 pytest
调用将共存很长时间,但在某些时候 py.test
可能会被弃用。所以我会推荐给#dropthedot。
[...] so from pytest 3.0 we will supported and recommend use of pytest as the main command instead of py.test. It's possible that in future we will deprecate py.test and potentially even remove it.
-- Dave Hunt
向后兼容性是 pytest 社区的一个非常重要的问题,因此旧方法可能永远不会消失,并且无论如何保留它都不会造成太大的维护负担(它只是在 [= 中被定义为不同的入口点) 18=]).