2014-12-07 46 views
2

我是Pytest的新手。我想测试我的意见,需要登录(用@login_required装饰)。
我有以下测试功能:烧瓶login_user不能与pytest一起使用

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 

其中client是:

@pytest.fixture(scope='session') 
def client(request, app): 
    return app.test_client() 

assert current_user == user回报True,但client.post回到登录页面,因为login_required重定向到登录页面。为什么会这样呢?做这件事的正确方法是什么?

回答

2

也许不是最简单的方法,但是:“在单元测试时全局关闭认证可以很方便,为了实现这一点,如果应用程序配置变量LOGIN_DISABLED或TESTING中的任何一个设置为True,这个装饰器将会是忽略“。通过https://pythonhosted.org/Flask-Security/api.html