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

后端 未结 4 866
闹比i
闹比i 2021-01-01 15:05

I\'m using flask to do register and login:

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

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


        
相关标签:
4条回答
  • 2021-01-01 15:34

    It seems that you have to wrap you testing calls with something like this:

    with self.app.test_client() as client:
        data = {'email': 'test@test', 'password': 'password'}
        rv = client.post('/2014-10-17/register', data=json.dumps(data))
        ...
    
    0 讨论(0)
  • 2021-01-01 15:36

    In my case, I've went to the flask.ctx.AppContext.__exit__ method and discovered that there was an invisible, unhandled exception in the exc_value argument which has somehow broken the whole thing to pieces.

    0 讨论(0)
  • 2021-01-01 15:48

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

    0 讨论(0)
  • 2021-01-01 15:52

    When your testA has a syntax error or other exceptions, the tearDown() method which does the context pop job will not be reached, so the testA's context wasn't popped correctly. Then your next test we call it testB will pop the testA's context. So, that's why you got the error AssertionError: Popped wrong request context..

    Check the error in your test code, fix it. Then the AssertionError will be gone automatically.

    0 讨论(0)
提交回复
热议问题