Nose: db setup and teardown

In the last post I noted some documentation related to nose and ORM. Well, they did not work for me because my setup was not exactly like others’. Here is what worked for me. In



  • I imported
    from pylons import config

    to get the SQLAlchemy engine embedded in pylons’s config variable

  • I imported
    import quickwiki.model as model

    so that I could get hold of my models and metadata

  • I created a class

    inheriting from


    to hold the setup and teardown code

  • In the

    method, I do


    to destroy all tables

  • In the

    method, I call


    to destroy tables if they have not already been cleaned up, and then call


    to create the tables.

Here is the final code:

"""Pylons application test package

When the test runner finds and executes tests within this directory,
this file will be loaded to setup the test environment.

It registers the root directory of the project in sys.path and
pkg_resources, in case the project hasn't been installed with
setuptools. It also initializes the application via websetup (paster
setup-app) with the project's test.ini configuration file.

import os
import sys
from unittest import TestCase

import pkg_resources
import paste.fixture
import paste.script.appinstall
from paste.deploy import loadapp
from routes import url_for

__all__ = ['url_for', 'TestController']

here_dir = os.path.dirname(os.path.abspath(__file__))
conf_dir = os.path.dirname(os.path.dirname(here_dir))

sys.path.insert(0, conf_dir)

test_file = os.path.join(conf_dir, 'test.ini')
cmd = paste.script.appinstall.SetupCommand('setup-app')[test_file])

from pylons import config
import quickwiki.model as model

class TestModel(TestCase):
We want the database to be created from scratch before each test and dropped
after each test (thus making them unit tests).

def setUp(self):
engine = config['pylons.g'].sa_engine

page = model.Page()
page.title = 'FrontPage'
page.content = 'Welcome to the QuickWiki front page'

def tearDown(self):

engine = config['pylons.g'].sa_engine

class TestController(TestModel):

def __init__(self, *args, **kwargs):
wsgiapp = loadapp('config:test.ini', relative_to=conf_dir) = paste.fixture.TestApp(wsgiapp)
TestCase.__init__(self, *args, **kwargs)

Well, that almost worked. I fell foul of the ‘setup-app’ command and ‘setup-config’ in As you can see, the ‘tests/’ file loads and executes the paster ‘setup-app’ command. Stands to reason: the app should be ‘set up’ before I run tests.

My setup-app is responsible for creating the DB and populating it with some initial data. But now I can’t repeat my tests, because the first time I run the tests, the db is created and initial data put in, and the next time I run the tests… poof:

IntegrityError: (IntegrityError) column title is not unique
u'INSERT INTO pages (title, content) VALUES (?, ?)' ['FrontPage',
'Welcome to the QuickWiki front page']

Well, I thought, I would just drop everything in the db in the tearDown and ensure that tearDown is called before setUp is run. No go. For some reason, it seems, the tearDown() is not working.

Well, it seems I must have at least one test, for the fixtures to be run. So I created a dummy test, and all was well.

Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: