gpt4 book ai didi

python - 'Flask'对象没有登录单元测试的属性 'post'错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:04:04 26 4
gpt4 key购买 nike

我正在尝试使用 Flask-Testing 测试我的登录功能。我正在关注 Flask docs on testing以及。 test_login() 函数引发 AttributeError: 'Flask' object has no attribute 'post'。为什么会出现此错误?

Traceback (most recent call last):
File "/home/lucas/PycharmProjects/FYP/Shares/tutorial/steps/test.py", line 57, in test_login_logout
rv = self.login('lucas', 'test') <br> <br>
File "/home/lucas/PycharmProjects/FYP/Shares/tutorial/steps/test.py", line 47, in login
return self.app.post('/login', data=dict(
AttributeError: 'Flask' object has no attribute 'post'
from flask.ext.testing import TestCase
from flask import Flask
from Shares import db
import manage

class test(TestCase):

def create_app(self):

app = Flask(__name__)
app.config['TESTING'] = True
return app

SQLALCHEMY_DATABASE_URI = "sqlite://"
TESTING = True

def setUp(self):
manage.initdb()

def tearDown(self):
db.session.remove()
db.drop_all()

def test_adduser(self):
user = User(username="test", email="test@test.com")
user2 = User(username="lucas", email="lucas@test.com")

db.session.add(user)
db.session.commit()

assert user in db.session
assert user2 not in db.session

def login(self, username, password):
return self.app.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)

def logout(self):
return self.app.get('/logout', follow_redirects=True)

def test_login(self):
rv = self.login('lucas', 'test')
assert 'You were logged in' in rv.data

最佳答案

看起来像Flask-Testing在名为 self.client 的 TestCase 实例上神奇地设置了一个特殊的应用程序客户端对象。将所有 self.app 更改为 self.client,它应该可以解决该问题。

例如:

def login(self, username, password):
return self.app.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)

到:

def login(self, username, password):
return self.client.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)

关于python - 'Flask'对象没有登录单元测试的属性 'post'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34913348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com