py.test 测试烧瓶寄存器,断言错误:弹出错误的请求上下文 [英] py.test to test flask register, AssertionError: Popped wrong request context
本文介绍了py.test 测试烧瓶寄存器,断言错误:弹出错误的请求上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用flask进行注册和登录:
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')
然后我使用 py.test 来测试类:
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
推荐答案
这是一个已知的flask 问题.您收到两个例外而不是一个.只需将 PRESERVE_CONTEXT_ON_EXCEPTION = False
添加到您的测试配置中即可.
It's a known flask problem. You receive two exceptions instead one. Simply add PRESERVE_CONTEXT_ON_EXCEPTION = False
to your test config.
这篇关于py.test 测试烧瓶寄存器,断言错误:弹出错误的请求上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文