测试覆盖Flask应用程序不起作用 [英] Test Coverage for Flask application doesnt work

查看:197
本文介绍了测试覆盖Flask应用程序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好想测试在我的瓶子应用程序在终端的删除路线我可以看到测试已经过去,它说test_user_delete(test_app.LayoutTestCase)...确定
但是当我打开封面还是红色,这意味着不包括它会请你有人向我解释为什么和我在做什么错了?

app.layout.view。 py




$ b $ 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: p>

  @ layout.route('/ delete /< int:id>')
def delete(id):
删除猴子
user = Users.query.get_or_404(id)
db.session.delete(用户)
db.session.commit()$ b格式(user.name))
返回重定向(url_for(layout.user,page = 1,sortby ='normal'))
({0}


解决方案

我假设您的设置方法设置 config ['Testing'] = True 。否则, Flask-login 会重定向到您的登录视图。


$ b 编辑成为这里的问题,因为你得到一个404错误。如果需要登录,问题是未经授权的 - 401错误。相反,我错误地认为你用'DELETE'方法注册了你的函数,所以我提供的url_for语句是错误的。

你可以在会话下找到闪烁的消息 _flashes 。你可以试试:

$ pre $ with self.client as c:
rv = self.client.get(url_for(' delete',id = e1.id),follow_redirects = True)
用c.session_transaction()打印rv.data
作为会话:
self.assertTrue(delete done! session ['_flashes'])

您也可以看看 Flask Testing


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'))

解决方案

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

这篇关于测试覆盖Flask应用程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆