简体   繁体   English

“py.test”vs“pytest”命令

[英]“py.test” vs “pytest” command

The py.test command is failing in my case, whereas pytest is running totally fine. 在我的情况下py.test命令失败,而pytest运行完全正常。

I use the pytest-flask plugin: 我使用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

When I invoke $ py.test I get the following error: 当我调用$ 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

This is my actual conftest.py file: 这是我的实际conftest.py文件:

import pytest
from app.app import create_app

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

My project structure is as like: 我的项目结构如下:

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

So, what is the difference between the two commands? 那么,这两个命令有什么区别? And why is one failing and the other not? 为什么一个失败而另一个失败?


update 1 更新1

1) I had to change my relative imports from .config import Config or from config import Config to absolute ones like from app.config import Config 1)我必须将相对导入from .config import Configfrom config import Config更改为绝对的from app.config import Config ,例如from app.config import Config

2) Running flask with python3 -m app.app 2)使用python3 -m app.app运行烧瓶

3) now pytest and py.test work normal 3)现在pytestpy.test工作正常

Thank you very much for your help, folks! 伙计们,非常感谢你们的帮助!


update 2 更新2

This is getting weird... when using absolute imports, running python with the -m option and flask with debug=True then the werkzeug library is not reloading the sources as expected: 这变得很奇怪......当使用绝对导入时,使用-m选项运行python并使用debug=True运行烧瓶,那么werkzeug库不会按预期重新加载源:

http://chase-seibert.github.io/blog/2015/06/12/flask-werkzeug-reloader-python-dash-m.html 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/werkzeug/issues/461

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

This helped me in my app/app.py : 这有助于我的app/app.py

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

Then python -m app.app works fine. 然后python -m app.app工作正常。

To answer the question about the actual command (meaning the invocation of the tool on the command line) in a bit more detail: 要更详细地回答关于实际命令的问题(意味着在命令行上调用工具):

The py.test invocation is the old and busted joint. py.test调用是旧的和破坏的联合。 pytest is the new hotness (since 3.0). pytest是新的pytest (从3.0开始)。 py.test and pytest invocations will coexist for a long time I guess, but at some point py.test might be deprecated. 我猜想py.testpytest调用会共存很长时间,但在某些时候py.test可能会被弃用。 So I would recommend to #dropthedot . 所以我建议#dropthedot

[...] so from pytest 3.0 we will supported and recommend use of pytest as the main command instead of py.test. [...]所以从pytest 3.0我们将支持并建议使用pytest作为主命令而不是py.test。 It's possible that in future we will deprecate py.test and potentially even remove it. 将来我们可能会弃用py.test甚至可能删除它。

-- Dave Hunt - 戴夫亨特

Backwards compatibility is a very important concern for the pytest community, so the old way might never go away and it's not much of a maintenance burden to keep it anyway (it is just defined as a different entry point in setup.py ). 向后兼容性对于pytest社区来说是一个非常重要的问题,因此旧的方式可能永远不会消失,无论如何都要保持它的维护负担(它只是定义为setup.py中的不同入口点)。

Use pytest ... or even better the python -m pytest ... . 使用pytest ...甚至更好的python -m pytest ...

You can forget about the old name, it's clearly a bug if you still spot it somewhere. 你可以忘记旧的名字,如果你仍然在某个地方发现它,这显然是一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM