作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我是 Pytest 的新手。我想测试需要登录的 View (用 @login_required
装饰)。
我有以下测试功能:
def test_add_new_post(self, client, user):
login_user(user)
assert current_user == user
data = {
'title': 'This is test post',
'body': 'This is test body'
}
client.post(url_for('posts.add_new'), data=data)
assert Post.query.count() == 1
客户端
所在的位置:
@pytest.fixture(scope='session')
def client(request, app):
return app.test_client()
assert current_user == user
返回 True
,但是 client.post
返回登录页面,因为 login_required
重定向到登录页面。为什么会发生这种情况,正确的做法是什么?
最佳答案
这对我有用(基于 this comment ):
def test_with_authenticated_user(app):
@app.login_manager.request_loader
def load_user_from_request(request):
return User.query.first()
# now you can call client.post or similar methods
关于python - Flask login_user 不适用于 pytest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27340913/
我是一名优秀的程序员,十分优秀!