python - 'Flask' object has no attribute 'post' error for login unit test -


i trying test log in functionality flask-testing. i'm following flask docs on testing well. test_login() function raises attributeerror: 'flask' object has no attribute 'post'. why getting 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 flask import flask 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 logged in' in rv.data 

it looks flask-testing magically sets special app client object on testcase instance named self.client. change self.app self.client , should fix issue.

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) 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -