Skip to content

Instantly share code, notes, and snippets.

@Ilgrim Ilgrim/README.rst forked from rduplain/README.rst
Created Jan 14, 2017

What would you like to do?
Demonstrate use of fixture with Flask-SQLAlchemy and Flask-Testing.

Demonstrate use of fixture with Flask-SQLAlchemy and Flask-Testing. February 13, 2011 Ron DuPlain <>

Post any feedback to:

Get this gist:

git clone git:// Flask-SQLAlchemy-Fixture
cd Flask-SQLAlchemy-Fixture

To run, activate a virtualenv and:

python develop

If you don't like DeprecationWarning, you can:

python -W ignore::DeprecationWarning

You can also:

python fixtures
sqlite3 app.db

In sqlite3:

select * from spams;
select * from eggs;

If you need a license for this code, use BSD.

from flask import Flask
import config
import models
app = Flask(__name__)
db = models.init_app(app)
SQLALCHEMY_DATABASE_URI = 'sqlite:///app.db'
from fixture import DataSet, SQLAlchemyFixture
from import NamedDataStyle
import models
def install(app, *args):
engine = models.create_tables(app)
db = SQLAlchemyFixture(env=models, style=NamedDataStyle(), engine=engine)
data =*args)
class SpamData(DataSet):
class spam01:
name = 'spam spam spam'
class EggData(DataSet):
class egg01:
description = 'green, for eating with mechanically separated meat'
# A simple trick for installing all fixtures from an external module.
all_data = (SpamData, EggData,)
from flaskext.script import Manager
from app import app
import fixtures as _fixtures
import models
manager = Manager(app)
def tables():
"Create database tables."
def fixtures():
"Install test data fixtures into the configured database."
_fixtures.install(app, *_fixtures.all_data)
if __name__ == "__main__":
from flaskext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
db = SQLAlchemy()
def init_app(app):
"""Initializes Flask app.""" = app
return db
def create_tables(app):
"Create tables, and return engine in case of further processing."
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
return engine
class Spam(db.Model):
__tablename__ = 'spams'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(16))
class Egg(db.Model):
__tablename__ = 'eggs'
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.Text)
from setuptools import setup
# Tested on Python 2.6 using the packages below.
import unittest
from flask import Flask
from flaskext.testing import TestCase
import fixtures
import models
class ModelsTestCase(TestCase):
db_uri = "sqlite:///tests.db"
def create_app(self):
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = self.db_uri
return app
def setUp(self):
fixtures.install(, *fixtures.all_data)
self.db = models.init_app(
def tearDown(self):
def test_spam(self):
spam = models.Spam.query.first()
self.assertEquals(, 'spam spam spam')
def test_egg(self):
egg = models.Egg.query.first()
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.