text: """ Defines testing fixtures.
  Modified from http://alexmic.net/flask-sqlalchemy-pytest/
  Also provides examples for managing (alembic) migrations, and using
  factories for creating an app.
  """
  import os
  import pytest
  from suddendev import create_app
  from suddendev.models import db as _db
  
  
  @pytest.fixture(scope='session')
  def app(request):
      """Session-wide test `Flask` application."""
      app = create_app()
  
      # Establish an application context before running the tests.
      ctx = app.app_context()
      ctx.push()
  
      def teardown():
          ctx.pop()
  
      request.addfinalizer(teardown)
      return app
  
  
  @pytest.fixture(scope='session')
  def db(app, request):
      """Session-wide test database."""
  
      def teardown():
          _db.drop_all()
  
      _db.app = app
      _db.create_all()
  
      request.addfinalizer(teardown)
      return _db
  
  
  @pytest.fixture(scope='function')
  def session(db, request):
      """Creates a new database session for a test."""
      connection = db.engine.connect()
      transaction = connection.begin()
  
      options = dict(bind=connection, binds={})
      session = db.create_scoped_session(options=options)
  
      db.session = session
  
      def teardown():
          transaction.rollback()
          connection.close()
          session.remove()
  
      request.addfinalizer(teardown)
      return session
  