问题
Hi want to test the "delete route" in my flask application in terminal I can see that the test is past and it said "test_user_delete (test_app.LayoutTestCase) ... ok" But when I open the cover page it still with red color which means doesn't cover it wold you please someone explain to me why and where I am doing wrong?
app.layout.view.py
test.py
e1 = Users(name='admine2', email='admine2@gmail.com', age=25)
e2 = Users(name='teste2', email='teste2@gmail.com', age=27)
db.session.add_all([e1, e2])
db.session.commit()
u = Users.query.get(1)
db.session.remove()
db.session.delete(u)
response = self.client.post('/delete/1',
follow_redirects=True)
self.assertTrue('admine2 is removed!', response.data)
view.py:
@layout.route('/delete/<int:id>')
def delete(id):
"""remove monkey"""
user = Users.query.get_or_404(id)
db.session.delete(user)
db.session.commit()
flash("{0} is removed!".format(user.name))
return redirect(url_for("layout.user", page=1, sortby='normal'))
回答1:
I assume your setup-method sets app.config['Testing'] = True
. Otherwise Flask-login is going to redirect you to your login-view.
Edit But this does not seem to be the issue here, as you get a 404-Error. If login-required was the problem is would be an unauthorized - 401 error. Instead I wrongfully assumed you registered your function with a 'DELETE' method, so my provided url_for statement was wrong.
You can find the flashed message in the session under the key _flashes
. You could try:
with self.client as c:
rv = self.client.get(url_for('delete', id=e1.id), follow_redirects=True)
print rv.data
with c.session_transaction() as session:
self.assertTrue("delete done !." in session['_flashes'])
You also might want to take a look at Flask Testing
来源:https://stackoverflow.com/questions/28670041/test-coverage-for-flask-application-doesnt-work