我正在尝试使用具有g的身份验证包装在API端点上运行pytest。但是,它始终将我的g.user对象打印为None
测试代码:
import pytest
from app import app
from flask import g
@pytest.fixture
def client():
app.secret_key = 'asasas'
app.testing = True
ctx = app.app_context()
ctx.push()
client = app.test_client()
yield client
ctx.pop()
def test_something(client):
g.user = {"_id": "test"}
resp = client.get('wizard/rules')
assert g.user == resp
这是认证包装器:
from functools import wraps
from flask import g, request, redirect, url_for
def authenticate(f):
@wraps(f)
def decorated_function(*args, **kwargs):
print(g.user)
if g.user is None or g.user.get('_id') is None:
return redirect(url_for('login', next=request.url))
return f(*args, **kwargs)
return decorated_function
在验证功能中,我有一个打印字符串,它打印g.user并一直打印None
。随着应用程序上下文的推送,我可以访问g并能够使用它,但是我无法绕过身份验证包装器。我也尝试过使用Faking resources and context中的语法,但它仍显示为None
。有人知道为什么吗?