我读了很长时间,但这是我第一次发帖。在
好的,所以我试着在Flask中对一个演示应用程序进行单元测试,我不知道我做错了什么。在
这些是我在一个名为经理.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
第一条路线是装货基本.html模板呈现一个“hi”消息,该消息在单元测试中正常工作,但第二个路由得到一个断言错误。在
这是我的测试文件管理_测试.py:
^{pr2}$此单元的输出为:
.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
更改为以下内容:请确保
from flask import request
。在还有一个例子,展示了它的工作原理:
^{2}$你的测试应该是:
编辑
根据您的评论:
但是,我不知道你为什么要用POST,因为这本质上是一个没有任何POST主体的POST。在
在这种情况下,我将取消对POST数据的所有要求:
使用GET的测试将是
或者,假设你有2.6+
相关问题 更多 >
编程相关推荐