jeudi 19 mai 2016

How to test login(which is using MySQL) in flask applicaton?

I've been learning flask following this tutorial. Now I'm trying to test some parts of the application with python unittest. But I can't grasp how to test user login corrctly. Here's the code for the test I'm trying to do:

from intro_to_flask import app
from models import db
from models import User

class BaseTestCase(unittest.TestCase):

def setUp(self):
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://credentials/database'
    app.config['TESTING'] = True
    app.config['CSRF_ENABLED'] = False
    with app.app_context():
        db.create_all()


def tearDown(self):
    with app.app_context():
        db.session.remove()
        db.drop_all()

def test_User_login(self):
    tester = app.test_client(self)
    u = User(firstname = 'testname', lastname = 'testlaname', email = 'test@example.com', password = 'pass')
    with app.app_context():
        db.session.add(u)
        db.session.commit()
    response = tester.post('/signin', data=dict(email = 'test@example.com', password = 'pass'), follow_redirects=True )
    self.assertIn(b'Profile', response.data) #User is redirected to profile page after loggin in which has giant Profile

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

The test fails and it's clear, that login and the following redirect doesn't happen.

Can someone explain to me, what is wrong with my test?

Python version: 2.7.9 MySQL version: 5.7 Flask version: 0.10.1 OS Windows 10

Aucun commentaire:

Enregistrer un commentaire