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

if some fail in teardown could be trigger _post_teardown error #21

Closed
dreampuf opened this issue May 19, 2013 · 14 comments
Closed

if some fail in teardown could be trigger _post_teardown error #21

dreampuf opened this issue May 19, 2013 · 14 comments

Comments

@dreampuf
Copy link

I custome my TestCase's teardown method, and it case a error.
When I run this case, it could raise a error about Flask-testing's self._ctx.pop() error (link) .
That make me can't find real problem except I note r85 .

When the teardown method raise a error, flask-testing execute the _post_teardown will case this error :

AssertionError: Popped wrong request context.  (<RequestContext 'http://localhost/connect/signup' [POST] of run> instead of <RequestContext 'http://localhost/' [GET] of run>)
@dreampuf
Copy link
Author

Could we add some fault-tolerance to the _post_teardown and else flask-testing methods to raise the real errors ?

@holms
Copy link

holms commented May 2, 2014

@jarus can you please take a look at this? I've also got this problem

@eriktaubeneck
Copy link

Setting PRESERVE_CONTEXT_ON_EXCEPTION = False in my config solved this.

@dwcaraway
Copy link

+1 to @eriktaubeneck , fixed my py.test, thanks!

@tkannippoyil
Copy link

@eriktaubeneck 💯 worked... :)

@rlam3
Copy link

rlam3 commented Jul 8, 2015

+1 this is still failing even with PRESERVE_CONTEXT_ON_EXCEPTION = False

emilyhorsman added a commit to emilyhorsman/nightshades-python that referenced this issue Jan 31, 2016
api.v1.update_unit and api.v1.show_unit now check to ensure the user
logged in is the user who created the resources.

Changed nightshades.api.get_unit to accept the user_id as a keyword
argument. This way the method is still useful when no user check is
deliberately needed, but it is still available.
nightshades.api.mark_complete is now handled in the same way.

The api.v1 Blueprint now has an error handler for peewee.DoesNotExist
exceptions so it will throw a JSON API 404. Which exceptions the
internal API can possibly throw should be reviewed and this exception
handling should be organized. I think that having the route actions
raise exceptions (instead of `abort`ing) followed by Flask errorhandler
decorators makes more sense.

PRESERVE_CONTEXT_ON_EXCEPTION config flag in the test client is due to:
jarus/flask-testing#21
@shankeygr
Copy link

PRESERVE_CONTEXT_ON_EXCEPTION = False, is not working for me

lukasjuhrich added a commit to lukasjuhrich/pycroft that referenced this issue Oct 25, 2016
This is necessary since in some situations, flask raises two exceptions
which confuses the request context.  See
http://stackoverflow.com/a/28139033/2443886 and
jarus/flask-testing#21
@xavierhardy
Copy link

I found what was causing the error in my case, the error handler itself had an error (which hasn't been raised).

@dreampuf
Copy link
Author

I used flask-testing years ago. This feature doesn't need anymore.
Feel free to re-open this, if you has the same issue.

@dasfrosty
Copy link

This is still an issue in the latest version of flask-testing (v0.6.2).

Setting PRESERVE_CONTEXT_ON_EXCEPTION = False does work to solve the issue though.

@dasfrosty
Copy link

Just to add some clarity, the AssertionError: Popped wrong request context error only happens when running the tests with the flask debug flag turned on.

This is because, from the flask config docs (http://flask.pocoo.org/docs/0.12/config/)...

By default if the application is in debug mode 
the request context is not popped on exceptions 
to enable debuggers to introspect the data.

So the other way to avoid this issue is to turn the debug flag off for testing:

export FLASK_DEBUG=0

@zoltan-fedor
Copy link

I started to receive the same errors too and none of the above suggestions has helped, not the PRESERVE_CONTEXT_ON_EXCEPTION = False nor the DEBUG flag being set to 0.

What did help in my case is turning off shared fixtures (scope="session") in pytest and setting their scope to function, which is the default.

This way my test runs became a bit less efficient, but now at least they don't error out randomly with the above error.

@harshal-c
Copy link

harshal-c commented Oct 12, 2021

I'm still facing this issue when I try to test background tasks using python-rq and have come across the same issue of AssertionError: Popped wrong request context.. I have applied all the suggested options still no good. Below are the fixtures which I use. Would appreciate if anyone can help with this.

I'm on python3.8 and using flask 2.0

P.S setting fixture scope to default also doesn't help the case

@pytest.fixture(scope='session')
def app():
    _app = create_app()
    _app.config["TESTING"] = True
    _app.config["PRESERVE_CONTEXT_ON_EXCEPTION"] = False
    _app.config["FLASK_DEBUG"] = False

    _app.test_request_context().push()

    yield _app


@pytest.fixture(scope='session')
def api(app):

    with app.test_client() as testing_client:
        _db.init_app(app)
        _db.create_all()

        yield testing_client
    
    _db.session.close()
    _db.drop_all()
    _db.get_engine(_db.app).dispose()

@pytest.fixture(scope='session')
def redis_worker(app, api):
    """Redis worker for processing jobs """

    queue = Queue(connection=Redis('127.0.0.1', 6300))
    return  SimpleWorker([queue], connection=queue.connection)

@jonasN5
Copy link

jonasN5 commented Feb 3, 2022

What did help in my case is turning off shared fixtures (scope="session") in pytest and setting their scope to function, which is the default.

This is the only thing that worked for me. Looking for the efficient way by preserving the session scope...

ruslan-simonenko added a commit to ruslan-simonenko/stc-ad-credit that referenced this issue Aug 4, 2023
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