Test Coverage for Flask application doesnt work

£可爱£侵袭症+ 提交于 2019-12-10 20:40:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!