2012-12-21 1 views
6

Sono stato lettore da molto tempo, ma questa è la prima volta che pubblicizzo.Errore unità di applicazione unità di test del flask

Ok, quindi sto provando a testare un'unità un'applicazione demo in Flask e non so cosa sto facendo male.

questi sono i miei "percorsi" in un file chiamato 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 

Il primo percorso sta caricando modello base.html rende un messaggio "hi", che sta lavorando nella unit test ma il secondo percorso ottiene un errore di asserzione.

e questo è il mio file di prova 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 

Questa è l'uscita dalla pista unità-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) 

vorrei sapere se voi ragazzi può aiutare me! cosa sto facendo male o che mi manca?

EDIT

ho fatto questo e che sta funzionando


    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

Per una cosa, è necessario allo w POST on/hello.Per un altro, l'argomento 'username' su' hello_username' non ottiene automaticamente i dati POST convertiti in argomenti del metodo. – sberry

+0

E per un'altra cosa, 't_username' non imposta i dati del post con l'argomento' username'. – yiding

risposta

2

si dovrebbe cambiare il vostro hello_username al seguente:

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

assicurati anche di from flask import request.

E un esempio, che mostra che funziona:

> 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 

E il test dovrebbe essere simile:

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

EDIT
Per il tuo commento:

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

Ma , quindi non lo faccio sai perché useresti il ​​POST poiché questo è essenzialmente un POST senza alcun corpo 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 

In tal caso, vorrei rimuovere il requisito per i dati POST tutti insieme:

@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 

il test utilizzando GET sarebbe

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

Oppure, a patto di avere 2.6+,

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

Grazie, ma perché devo modificare il "percorso" e il metodo per correggere la richiesta? So che forse è la stessa cosa ... ma mi confonde in questo modo – albertogg

+0

Se stai cercando di accettare i dati POST, allora il percorso deve accettare il metodo POST. È solo HTTP. – sberry

+0

hai ragione con de POST, ma se cambio il "percorso" smetterà di funzionare nella mia app, questo è il mio caso – albertogg