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
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
E per un'altra cosa, 't_username' non imposta i dati del post con l'argomento' username'. – yiding