It seems common practice in Flask to start like this:
from flask import Flask
from flaskext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
SQLALCHEMY_DAT
First, instead of instantiating Flask app directly in your script, you use an application factory. It means you create a function that takes your config file as parameter, and return the instantiated app object. Then, you create the global SQLAlchemy object without parameter, and you configure it when creating the app, as explained here.
db = SQLAlchemy()
def create_app(configfile):
app = Flask(__name__)
app.config.from_pyfile(config, silent=True)
db.init_app(app)
# create routes, etc.
return app
To run the app, you simply do something like:
app = create_app('config.py')
app.run()
To run unittests, you can do something like:
class Test(TestCase):
def setUp(self):
# init test database, etc.
app = create_app('test_config.py')
self.app = app.test_client()
def tearDown(self):
# delete test database, etc.
In my case, I'm using SQLAlchemy directly with scoped_session instead of Flask-SQLAlchemy. I did the same, but with Lazy SQLAlchemy setup.