[Flask] Flask-SQLAlchemy create database once per test case

Jesvin Jose frank.einstien at gmail.com
Tue Sep 22 13:31:59 CEST 2015


I have the following base test class that enables Flask-SQLAlchemy, and
creates a clean database and session for each test.

from app import app, db
class BaseTest(unittest.TestCase):
    def setUp(self):
        self.app_context = app.app_context()
        self.app_context.push()
        db.create_all()
        db.session.begin_nested()

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

The disadvantage is that I pay the overhead of DDL for every test* method
in the class.

Could you help with that problem? I want the following structure:

setUpClass - set up app context (whatever that is - without it I notice
that db wont work) and run DDL commands
setUp - create a nested transaction
tearDown - reset the session for the next test
tearDownClass - remove app context and database

-- 
We dont beat the reaper by living longer. We beat the reaper by living well
and living fully. The reaper will come for all of us. Question is, what do
we do between the time we are born and the time he shows up? -Randy Pausch
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20150922/f2b53ea8/attachment.html>


More information about the Flask mailing list