2012-12-21 64 views
6

我已經很長時間的讀者,但這是我第一次發佈。燒瓶應用單元 - 測試斷言錯誤

好的,所以我想單元測試Flask中的演示應用程序,我不知道我做錯了什麼。

這些都是我的「路線」在一個名爲manager.py文件:

@app.route('/') 
@app.route('/index') 
def hello(): 
    return render_template('base.html') 


@app.route('/hello/<username>') 
def hello_username(username): 
    return "Hello %s" % username 

第一條路線是加載base.html文件模板呈現一個「喜」的消息,這是在工作單位 - 測試但第二條路線得到斷言錯誤。

,這是我的測試文件manage_test.py

class ManagerTestCase(unittest.TestCase): 

    def setUp(self): 
     self.app = app.test_client() 

    def t_username(self, username): 
     return self.app.post('/hello/<username>', follow_redirects=True) 

    def test_username(self): 
     rv = self.t_username('alberto') 
     assert "Hello alberto" in rv.data 

    def test_empty_db(self): 
     rv = self.app.get('/') 
     assert 'hi' in rv.data 

這是從單元測試運行的輸出:

.F 
====================================================================== 
FAIL: test_username (tests.manage_tests.ManagerTestCase) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/Users/albertogg/Dropbox/code/Python/flask-bootstrap/tests/manage_tests.py", line 15, in test_username 
    assert "Hello alberto" in rv.data 
AssertionError 

---------------------------------------------------------------------- 
Ran 2 tests in 0.015s 

FAILED (failures=1) 

我想知道,如果你們能幫助我!我做錯了什麼或失蹤?

編輯

我這樣做,它的工作


    class ManagerTestCase(unittest.TestCase):

def setUp(self): 
     self.app = app.test_client() 

    def t_username(self, username): 
     return self.app.get('/hello/%s' % (username), follow_redirects=True') 
     # either that or the Advanced string formatting from the answer are working. 

    def test_username(self): 
     rv = self.t_username('alberto') 
     assert "Hello alberto" in rv.data 

    def test_empty_db(self): 
     rv = self.app.get('/') 
     assert 'hi' in rv.data 

+0

首先,你需要allo在POST上/你好。另一方面,'hello_username'的'username'參數不會自動將POST數據轉換爲方法參數。 – sberry

+0

另一方面,'t_username'不會使用'username'參數設置帖子的數據。 – yiding

回答

2

你應該改變你的hello_username以下幾點:

@app.route('/hello/', methods=['POST']) 
def hello_username(): 
    return "Hello %s" % request.form.get('username', 'nobody') 

請確保from flask import request也。

和示例,顯示它的工作:

> curl -X POST -i 'http://localhost:2000/hello/' -d "username=alberto" 
HTTP/1.0 200 OK 
Content-Type: text/html; charset=utf-8 
Content-Length: 9 
Server: Werkzeug/0.8.3 Python/2.7.2 
Date: Fri, 21 Dec 2012 05:42:49 GMT 

Hello alberto 

而且您的測試應該是這樣的:

def test_username(self, username): 
    return self.app.post('/hello', data={"username":username}) 

編輯
每您的評論:

@app.route('/hello/<username>', methods=['POST']) 
def hello_username(username): 
    print request.args 
    return "Hello %s" % username 

但,那麼我不會知道爲什麼你會使用POST,因爲這實際上是一個沒有任何POST主體的POST。

> curl -X POST -i 'http://localhost:2000/hello/alberto'   
HTTP/1.0 200 OK 
Content-Type: text/html; charset=utf-8 
Content-Length: 13 
Server: Werkzeug/0.8.3 Python/2.7.2 
Date: Fri, 21 Dec 2012 06:29:25 GMT 

Hello alberto 

在這種情況下,我會刪除的要求進行POST數據一起:

@app.route('/hello/<username>', methods=['POST']) 
def hello_username(username): 
    print request.args 
    return "Hello %s" % username 


> curl -i 'http://localhost:2000/hello/alberto'   
HTTP/1.0 200 OK 
Content-Type: text/html; charset=utf-8 
Content-Length: 13 
Server: Werkzeug/0.8.3 Python/2.7.2 
Date: Fri, 21 Dec 2012 06:31:10 GMT 

使用GET的測試將是

def test_username(self, username): 
    return self.app.get('/hello/%s' % (username), follow_redirects=True) 

或者,假設你有2.6+,

def test_username(self, username): 
    return self.app.get('/hello/{username}'.format(username=username), follow_redirects=True) 
+0

謝謝,但爲什麼我必須修改「路由」和方法以修復請求?我知道也許這是同樣的事情......但它會讓我困惑 – albertogg

+0

如果您嘗試接受POST數據,則路由需要接受POST方法。這只是HTTP。 – sberry

+0

你是對de POST,但如果我改變「路線」它將停止在我的應用程序工作,這是我的情況 – albertogg