py.test to test flask register, AssertionError: Popped wrong request context

Spirit picture Spirit · Oct 30, 2014 · Viewed 8.5k times · Source

I'm using flask to do register and login:

from flask.ext.security.views import register, login

class Register(Resource):
    def post(self):
        return register()

class Login(Resource):
    def post(self):
        return login()

api.add_resource(Login, '/login')
api.add_resource(Register, '/register')

then I use py.test to test the class:

class TestAPI:
    def test_survey(self, app):
        client = app.test_client()
        data = {'email': 'test@test', 'password': 'password'}
        rv = client.post('/2014-10-17/register',
                          data=json.dumps(data))
        ...

when I ran the test, the error occurred as follow:

AssertionError: Popped wrong request context.  (<RequestContext 'http://localhost/2014-10-17/register' [POST] of panel.app> instead of <RequestContext 'http://localhost/' [GET] of panel.app>)

Do you know why? And when testing login, there was no such error

Answer

Jimilian picture Jimilian · Jan 25, 2015

It's a known flask problem. You receive two exceptions instead one. Simply add PRESERVE_CONTEXT_ON_EXCEPTION = False to your test config.