【问题标题】:Flask-testing : self.client.delete() return 200 but fail as he doesnt delete anything烧瓶测试:self.client.delete() 返回 200 但失败,因为他没有删除任何内容
【发布时间】:2018-12-27 20:52:31
【问题描述】:

我有一个使用 SQL Alchemy 和 postgresql 的 CRUD 足球运动员烧瓶应用程序。 我正在尝试使用 unittest 和 flask-testing 对其进行测试,但是当我在播放器上测试删除和修补请求时(基于他的编号),返回的状态为 200,但播放器并未从数据库中删除,因此测试失败.

我的 app.py :

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='postgresql://postgres:password@localhost:5432/flask-sql-alchemy'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
modus = Modus(app)


class om_player(db.Model):

    __tablename__ = "players"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text)
    number = db.Column(db.Integer)
    age = db.Column(db.Integer)

    def __init__(self, name, number, age):
        self.name = name
        self.number = number
        self.age = age

@app.route('/joueurs/<int:number>', methods=["GET", "PATCH", "DELETE"])
def show(number):
    found_player = om_player.query.filter_by(number=number).first()

    if request.method == b"PATCH":
        found_player.name = request.form['name']
        found_player.number = request.form['number']
        found_player.age = request.form['age']
        db.session.add(found_player)
        db.session.commit()
        return redirect(url_for('show', number=request.form['number']))
    if request.method == b"DELETE":
        db.session.delete(found_player)
        db.session.commit()
        return redirect(url_for('joueurs'))
    return render_template('show.html', player=found_player)

我的 test.py:

from app import app,db, om_player
from flask_testing import TestCase
import unittest

class BaseTestCase(TestCase):
    def create_app(self):
        app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost:5432/flask-sql-alchemy'
        app.config['PRESERVE_CONTEXT_ON_EXCEPTION'] = False
        return app

    def test_delete(self):
        response = self.client.delete('/joueurs/18', follow_redirects=True)
        self.assertEqual(response.status_code, 200)
        self.assertNotIn(b"AMAVI", response.data)


if __name__ == '__main__':
    unittest.main()

(joueurs是法语玩家的意思)

当我运行 python test.py 时:

AssertionError: b'AMAVI' 在 b'my edit.html file' 中意外发现

所以编号为 18 且名为“AMAVI”的播放器没有被删除,我不明白为什么,因为删除请求返回 200 并且我的应用在本地服务器上运行良好。

【问题讨论】:

    标签: python flask sqlalchemy python-unittest flask-testing


    【解决方案1】:

    如果您使用的是 Python3,b"PATCH"b"DELETE" 将不再符合您的预期。这些测试因“POST”而失败并进入默认的“GET”情况。

    评估

    "POST" == b"POST"
    

    在 Python3 REPL 中进行确认。

    【讨论】:

    • 谢谢,您发现问题出在哪里,“POST” == b"POST" 在我的 Python3 REPL 中返回 False。在我的删除和更新请求中没有“b”,测试工作。但是flask-modus返回字节文字,如果我在没有'b'的情况下运行我的应用程序,我会被重定向并且播放器不会被删除。我尝试添加 if request.method == b"DELETE" 或 "DELETE" 但我的应用程序仍然存在错误(当我只想更新他们时,玩家会被删除......)。是否有任何解决方案可以使应用程序和测试工作?
    • 我快速浏览了 flask-modus 的源代码,并没有看到任何迹象表明它会返回字节而不是字符串,但如果我遗漏了什么,请尝试 if request.method in [b"DELETE", "DELETE"]。跨度>
    • 我在我的烧瓶课程中读到了它:crud-with-flask-course 但是是的,我也没有在烧瓶模式文档中找到它。非常感谢您的帮助,应用程序和测试现在完美运行。
    猜你喜欢
    • 1970-01-01
    • 2017-01-11
    • 2021-06-03
    • 2019-01-06
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2019-09-11
    • 2019-04-09
    相关资源
    最近更新 更多