Skip to content

pytest blows up when collecting modules with uninspectable objects #214

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

Closed
pytestbot opened this issue Oct 31, 2012 · 9 comments
Closed
Labels
type: bug problem that needs to be addressed

Comments

@pytestbot
Copy link
Contributor

Originally reported by: Thomas Waldmann (BitBucket: thomaswaldmann, GitHub: thomaswaldmann)


werkzeug/flask offers some threadlocals to comfortable access stuff like (http) request, session, (wsgi) app, etc. - those objects are special and only valid when a request is running or an app was created.

if you access them before / after they are valid, it'll raise a RuntimeError.

while collecting tests, pytest (since 2.3) accesses all module-level objects and tries to inspect them for a specific attribute, causing a RuntimeError exception when it does that on such special objects.

thus, the usual code used in about every flask app makes pytest unusable:

from flask import request, session # <- threadlocals

a simple patch that solves the issue is attached.


@pytestbot
Copy link
Contributor Author

Original comment by Thomas Waldmann (BitBucket: thomaswaldmann, GitHub: thomaswaldmann):


maybe fix the comment in the patch to say "flask/werkzeug's threadlocals".
the implementation is in werkzeug, but (IIRC) only flask uses session/request.

@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


its missing a test

i think we should catch the specific exceptions, will fix

@pytestbot
Copy link
Contributor Author

Original comment by Thomas Waldmann (BitBucket: thomaswaldmann, GitHub: thomaswaldmann):


well, i manually tested it, with a flask import, but i doubt you want a flask dependency in pytest's tests.

@pytestbot
Copy link
Contributor Author

Original comment by Thomas Waldmann (BitBucket: thomaswaldmann, GitHub: thomaswaldmann):


i first had a more specific exception handler, but then decided to rather be more general. you basically don't know what exceptions could happen.

usually one would go for an AttributeError at that place, but then the threadlocals would still blow up things, as they raise RuntimeError.

@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


i meant to catch only a runtimeerror, and test it with a fake object

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


I think it's safe to catch more generically (except KeyboardInterrupt etc.). This particular issue is about parsing fixtures - if for some very unlikely reason there is a real issue underlying the exception, the according pytest-fixture function wil not be available and cause other problems.

@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


fix issue 214 - gracefully handle proxy objects that look like fixtures

@pytestbot
Copy link
Contributor Author

Original comment by Ronny Pfannschmidt (BitBucket: RonnyPfannschmidt, GitHub: RonnyPfannschmidt):


i currently only tested agains RuntimeError,
i'd like to keep it as specific as possible, since i'd like to avoid hiding real errors

@pytestbot
Copy link
Contributor Author

Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42):


extended - fix issue214 - ignore attribute-access errors with objects in test modules that can blow up (for example flask's request object)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

1 participant