Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest fails if run from inside a function #1110

Closed
kmkurn opened this issue Oct 4, 2015 · 2 comments
Closed

Pytest fails if run from inside a function #1110

kmkurn opened this issue Oct 4, 2015 · 2 comments

Comments

@kmkurn
Copy link

kmkurn commented Oct 4, 2015

Hi,

I have a Flask application with directory structure like this:

project_dir/
|-- manage.py
|-- myapp/
|   |-- __init__.py
|-- tests/
    |-- test_myapp.py  # here I do import myapp and pytest

I am using Flask-Script plugin so I can do python manage.py test to run pytest. I have this in my manage.py file:

from flask.ext.script import Manager
import pytest

from myapp import app

manager = Manager(app)


@manager.command
def test():
    pytest.main()

if __name__ == '__main__':
    manager.run()

And when I run python manage.py test it fails with ERROR: file not found: test. Turns out pytest interprets the function name as a file name and it fails because the file does not exist.

Strangely, if I change the function slightly:

@manager.command
def test():
    pytest.main('-v')  # Run in verbose mode

It runs perfectly! I don't even have to install myapp in editable mode nor set the PYTHONPATH environment variable (which is weird since if I run py.test on project_dir it fails to import myapp). What's happening here?

I am using OS X 10.11, Python 3.4.3, and here's my pip freeze output:

appnope==0.1.0
beautifulsoup4==4.4.1
click==5.1
decorator==4.0.4
flake8==2.4.1
Flask==0.10.1
Flask-Script==2.0.5
gnureadline==6.3.3
gunicorn==19.3.0
ipython==4.0.0
ipython-genutils==0.1.0
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
mccabe==0.3.1
path.py==8.1.1
pep8==1.5.7
pep8-naming==0.3.3
pexpect==3.3
pickleshare==0.5
py==1.4.30
pyflakes==0.8.1
pytest==2.8.0
python-dotenv==0.1.3
PyYAML==3.11
requests==2.7.0
simplegeneric==0.8.1
traitlets==4.0.0
Werkzeug==0.10.4
wheel==0.24.0

Any help will be appreciated. Thank you.

@RonnyPfannschmidt
Copy link
Member

pytest.main will use sys.argv unless you pass a own list of options,

using pytest.main([]) should be sufficient for the simple case
you might want to pass additional arguments for test selection/pytest options

@kmkurn
Copy link
Author

kmkurn commented Oct 4, 2015

@RonnyPfannschmidt thanks for the explanation. now it makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants