"py.test" vs "pytest" command

swiesend picture swiesend · Sep 14, 2016 · Viewed 17.2k times · Source

The py.test command is failing in my case, whereas pytest is running totally fine.

I use the pytest-flask plugin:

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:

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:

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) 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

2) Running flask with python3 -m app.app

3) now pytest and py.test work normal

Thank you very much for your help, folks!


update 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:

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

This helped me in my app/app.py:

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

Then python -m app.app works fine.

Answer

Oliver Bestwalter picture Oliver Bestwalter · Jan 27, 2017

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. pytest is the new hotness (since 3.0). py.test and pytest invocations will coexist for a long time I guess, but at some point py.test might be deprecated. So I would recommend to #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

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).