2014-03-12 93 views
0

爲我正忙於編寫的API編寫一些單元測試,試圖在我的單元測試中設置一個會話以模擬已登錄的用戶。 由於某些原因,它看起來不像會話get傳遞通過client.get發送到api端點。任何人都知道我在做什麼錯了?unittest傳遞會話

client = app.test_client() 
    with client.session_transaction() as sess: 
     sess['username'] = '[email protected]' 
     response = client.get('/users/') 

     # Looks good 
     # <SecureCookieSession {'username': '[email protected]'}> 
     print sess 

     # Nothing in the cookie... 
     print response.data 

     # self.assertEquals(
     #  json.loads(response.data), 
     #  json.loads('{"users": "all"}')) 

回答

2

你需要移動client.get()with上下文管理的:

with app.test_client() as client: 
    with client.session_transaction() as sess: 
     sess['username'] = '[email protected]' 

    response = client.get('/users/') 

只在with塊的結尾 '承諾' 的會議。

+0

謝謝,現在完美工作 – daemonza