Error confirming application for a bottle

I read for a long time, but this is my first publication.

Ok, so I'm trying to use the unit test 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. The template displays a hello message that works in the unit test, but the second route receives an approval error.

and this is my manage_test.py testing file :

 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 result of a single test:

 .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 not enough?

EDIT

I did it and it worked

 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 

+6
source share
1 answer

You should change your hello_username to the following:

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

required from flask import request .

And an example showing that it works:

 > 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 

And your test should look like this:

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

EDIT
For your comment:

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

But, then I do not know why you are using POST, since it is essentially POST without any POST body.

 > 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 

In this case, I will remove the requirement for the POST data together:

 @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 

Test using get will

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

Or, if you have 2.6+,

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

All Articles