[英]Flask Application Unit-Test Assertion Error
我已經很長時間了,但這是我第一次發貼。
好的,所以我試圖在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模板,呈現一個“ hi”消息,該消息正在單元測試中運行,但是第二個路徑得到了斷言錯誤。
這是我的測試文件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
您應該將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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.