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
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
Innymi słowy, 't_username' nie ustawia danych posta argumentem' username'. – yiding