3

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

enter image description here

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'))
LiLi
  • 301
  • 2
  • 7
  • 21
  • Try to change `self.assertTrue('delete done!', response.data)` by `self.assertEqual('delete done!', response.data)`: you are not testing anything now your `self.assertTrue()` sentence is always true. I'm not a Falsh expert but as far as I know `flash()` message are not in response but you can find somewhere else. – Michele d'Amico Feb 23 '15 at 11:59
  • thnx @Micheled'Amico I got this error AssertionError: 'delete done!' != ' \n404 Not Found\n

    Not Found

    \n

    The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

    \n'
    – LiLi Feb 23 '15 at 12:02
  • That means that you are not calling delete page (as coverage suggested). First test `response.code==200` (I'm guessing ...) to take sure to call it and the test db for user. – Michele d'Amico Feb 23 '15 at 12:06
  • response = self.client.get(url_for('layout.delete%d'.format(e1.id) ')) self.assertEqual(response.status_code, 200) @Micheled'Amico I'm not expert too :(( it should be like this? – LiLi Feb 23 '15 at 12:21
  • Take a look to http://flask.pocoo.org/docs/0.10/testing/ and http://stackoverflow.com/questions/15278285/setting-mocking-request-headers-for-flask-app-unit-test – Michele d'Amico Feb 23 '15 at 12:33
  • well I read them before @Micheled'Amico But not really help – LiLi Feb 23 '15 at 13:02
  • Try to use `app = flaskr.app.test_client()` and `response = app.get('layout.delete%d'.format(e1.id))` – Michele d'Amico Feb 23 '15 at 13:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71495/discussion-between-lili-and-michele-damico). – LiLi Feb 23 '15 at 13:35
  • Code coverage works fine. I think mistake associated with `login_required`. You can check it. Just comment `login_required` line. And re-run your test. If it helps, it means you should login before test. Also, you can use `url_for(delete, id=9)` instead hard coded `/layout.delete/%d'.format(e1.id)` in tests too. It may helps you, because `url_for` guarantee correct url. – Jimilian Feb 23 '15 at 19:16

1 Answers1

-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

philmaweb
  • 514
  • 7
  • 13