简体   繁体   English

对于登录单元测试,'Flask'对象没有属性'post'错误

[英]'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.

相关问题 出现错误:AttributeError: &#39;Flask&#39; 对象没有属性 &#39;login_manager&#39; - Getting an error: AttributeError: 'Flask' object has no attribute 'login_manager' Flask:“令牌”object 没有属性“测试”| 渲染模板错误 - Flask : 'Token' object has no attribute 'test' | render_template error Flask 单元测试抛出错误:在 Jinja2 模板中没有属性“csrf_token” - Flask unit test throwing Error: has no attribute 'csrf_token' in Jinja2 template 我该如何解决错误:AttributeError: 'Flask' object has no attribute 'login_manager' - How can i fix the error: AttributeError: 'Flask' object has no attribute 'login_manager' Flask 错误:“NoneType” object 没有属性“永久” - Flask Error : 'NoneType' object has no attribute 'permanent' Python flask - object 没有属性错误 - Python flask - object has no attribute error flask 错误“NoneType” object 没有属性“密码” - flask error 'NoneType' object has no attribute 'password' 属性错误:'str' object 没有属性'post' - Attribute error : 'str' object has no attribute 'post' 在 Flask-SQLAlchemy 错误中一次更新多行:AttributeError:'InstanceState' object 没有属性'_post_inspect' - Updating multiple rows at once in Flask-SQLAlchemy Error: AttributeError: 'InstanceState' object has no attribute '_post_inspect' AttributeError: 'Flask' object 没有属性 'login_manager' - AttributeError: 'Flask' object has no attribute 'login_manager'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM