title: flask_sqlalchemy create_all() fails silently in unit testing
body: <p>I'm writing unit tests for a REST API written in Flask with the flask_sqlalchemy extension. Because I have a number of model classes, I wrote a <code>TestCase</code> subclass to do the standard setUp/cleanUp of the test database. All my test classes inherit from this. Each test succeeds when run alone, but when I run more than one test in a single class, the second setUp() fails on the <code>self.db.session.commit()</code> (I'm trying to add an entry to the User table) because <code>self.db.create_all()</code> has (silently) failed to create any tables.
  Here is my base test class, in the <code>__init__.py</code> of the test package:</p>
  
  <pre><code>import unittest
  
  from .test_client import TestClient
  from .. import create_app
  from pdb import set_trace as DBG
  
  class ApiTest(unittest.TestCase):
      default_username = 'fred'
      default_password = 'bloggs'
      db = None
  
      def setUp(self):
          try:
              self.app = create_app('testing')
              self.addCleanup(self.cleanUp)
              self.ctx = self.app.app_context()
              self.ctx.push()
              from .. import db
              self.db = db
              self.db.session.commit()
              self.db.drop_all(app=self.app)
              from ..models import User, Player, Team, Match, Game
              # self.app.logger.debug('drop_all())')
              self.db.create_all(app=self.app)
              # self.app.logger.debug('create_all())')
              user = User(user_name=self.default_username)
              user.password = self.default_password
              self.db.session.add(u)
              self.db.session.commit()
              self.client = TestClient(self.app, user.generate_auth_token(), '')
          except Exception, ex:
              self.app.logger.error("Error during setUp: %s" % ex)
              raise
  
      def cleanUp(self):
          try:
              self.db.session.commit()
              self.db.session.remove()
              self.db.drop_all(app=self.app)
              # self.app.logger.debug('drop_all())')
              self.ctx.pop()
          except Exception, ex:
              self.app.logger.error("Error during cleanUp: %s" % ex)
              raise
  </code></pre>
  
  <p>Can anyone tell me what's wrong here please?</p>
  
  <p>EDIT: Added the code for <code>create_app()</code> as requested.</p>
  
  <pre><code># chessleague/__init__.py
  import os
  
  from flask import Flask, g
  from flask_sqlalchemy import SQLAlchemy
  from flask_login import LoginManager
  
  
  from . import config
  
  
  app = None
  db = None  # The database, initialised in create_app()
  
  
  def create_app(config_name):
      app = Flask(__name__)
      app.config.update(config.get_config(config_name))
      # if app.config['USE_TOKEN_AUTH']:
      #     from api.token import token as token_blueprint
      #     app.register_blueprint(token_blueprint, url_prefix='/auth')
      import logging
      from logging.handlers import SysLogHandler
      syslog_handler = SysLogHandler()
      syslog_handler.setLevel(logging.WARNING)
      app.logger.addHandler(syslog_handler)
  
      login_manager = LoginManager()
      login_manager.login_view = 'auth.login'
      login_manager.init_app(app)
      global db
      db = SQLAlchemy(app)
      db.init_app(app)
      from .models import User,Player,Game,Match,Team,Post
      db.create_all()
      from .api import api as api_blueprint
      app.register_blueprint(api_blueprint, url_prefix='/chessleague')
      return app
  </code></pre>
  
  <p>`</p>