2012-12-21 14 views
6

Jestem od dawna czytelnikiem, ale to jest moja pierwsza publikacja.Flask Application Unit-Test asercja Error

Ok, więc staram się przetestować aplikację demonstracyjną w Flasku i nie wiem, co robię źle.

to są moje „drogi” w pliku o nazwie 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 

Pierwsza trasa się ładuje szablon base.html renderuje komunikat „Hi”, który pracuje w Unit- test, ale druga trasa otrzymuje błąd potwierdzenia.

i to jest mój plik testowanie 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 

Jest to wyjście z biegu jednostka testu:

.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) 

Chcę wiedzieć, czy macie może pomóc mnie! co robię źle lub zaginę?

EDIT

Zrobiłem to i to działa


    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

Po pierwsze trzeba zrobić alo w POST on/hello.Po drugie, argument 'username' na' hello_username' nie powoduje, że dane POST są automatycznie konwertowane na argumenty metody. – sberry

+0

Innymi słowy, 't_username' nie ustawia danych posta argumentem' username'. – yiding

Odpowiedz

2

Należy zmienić hello_username na następujące kwestie:

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

Upewnij się także, że from flask import request.

a przykładem, pokazując go pracy:

> 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 

i Twojego testu powinna wyglądać następująco:

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

EDIT
Za swój komentarz:

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

Ale , a potem nie wiedzieć, dlaczego używałeś POST, ponieważ jest to zasadniczo POST bez ciała 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 

W tym przypadku, chciałbym usunąć wymóg dla danych POST wszystko razem:

@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 

Badanie za pomocą GET byłoby

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

Albo, zakładając, że masz 2.6+,

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

Dzięki, ale dlaczego muszę zmodyfikować "trasę" i metodę, aby naprawić żądanie? Wiem, że to jest to samo ... ale to mnie myli w ten sposób – albertogg

+0

Jeśli próbujesz zaakceptować dane POST, to trasa musi zaakceptować metodę POST. To tylko HTTP. – sberry

+0

masz rację z de POST, ale jeśli zmienię "trasę", to przestanie działać w mojej aplikacji, to jest mój przypadek – albertogg