I’ve been long time reader, but this is my first time posting.
Ok, so I’m trying to unit test a demo application in Flask and I don’t know what I’m doing wrong.
these are my “routes” in a file called 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
The first route is loading base.html template renders a “hi” message, which is working in the unit-test but the second route gets an assertion error.
and this is my testing file 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
This is the output from the unit-test run:
.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)
I want to know if you guys can help me! what am I doing wrong or missing?
EDIT
I did this and it’s working
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
You should change your
hello_usernameto the following:make sure to
from flask import requestalso.And an example, showing it working:
And your test should look like:
EDIT
Per your comment:
But, then I don’t know why you would be using POST since this is essentially a POST without any POST body.
In that case, I would remove the requirement for POST data all together:
The test using GET would be
Or, assuming you have 2.6+,