2014-12-07 33 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