[英]'Flask' object has no attribute 'post' error for login unit test
I am trying to test my log in functionality with Flask-Testing. 我正在尝试使用Flask-Testing测试我的登录功能。 I'm following the Flask docs on testing as well.
我也在关注测试的Flask文档 。 The
test_login()
function raises AttributeError: 'Flask' object has no attribute 'post'
. test_login()
函数引发AttributeError: 'Flask' object has no attribute 'post'
。 Why am I getting this error? 为什么我收到此错误?
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
It looks like Flask-Testing magically sets up a special app client object on the TestCase instance named self.client
. 看起来Flask-Testing在名为
self.client
的TestCase实例上神奇地设置了一个特殊的app客户端对象。 Change all self.app
to self.client
and it should fix that issue. 将所有
self.app
更改为self.client
,它应该解决该问题。
Eg: 例如:
def login(self, username, password):
return self.app.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)
to: 至:
def login(self, username, password):
return self.client.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)
While writing tests for my Flask app I came across a similar problem. 在为我的Flask应用程序编写测试时,我遇到了类似的问题。
Only from debugging I saw that there was no "config" attribute instead I had to go self.app.application.config
仅从调试我看到没有“config”属性,而是我必须去
self.app.application.config
Not sure why it's missing, I've usually always done self.app.config
just like in the production code 不知道为什么它丢失了,我通常总是像生产代码一样完成
self.app.config
import unittest
from factory import create_app
class ConfigTests(unittest.TestCase):
def setUp(self):
app = create_app('flask_test.cfg')
app.testing = True
self.app = app.test_client()
def test_app_is_development(self):
self.assertFalse(self.app.application.config['SECRET_KEY'] is 'secret_key')
self.assertTrue(self.app.application.config['DEBUG'] is True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.