Skip to content

Instantly share code, notes, and snippets.

@Mistat
Created August 23, 2012 01:48
Show Gist options
  • Save Mistat/3431193 to your computer and use it in GitHub Desktop.
Save Mistat/3431193 to your computer and use it in GitHub Desktop.
pyramid test case sample
# -*- coding: utf-8 -*-
import sys
import unittest
import pyramid.threadlocal
import pyramid.testing
import pyramid.paster
import sqlalchemy
import sqlahelper
def db_init():
DSN = pyramid.threadlocal.get_current_registry().settings['sqlalchemy.url']
sys.stderr.write("\033[0;31m*** debug: DSN=%r\033[0m\n" % (DSN, ))
DBSession = sqlahelper.get_session()
Base = sqlahelper.get_base()
engine = sqlalchemy.create_engine(DSN)
#Base.metadata.create_all(engine)
DBSession.configure(bind=engine)
return DBSession
class Example_TC(unittest.TestCase):
def setUp(self):
# http://docs.pylonsproject.org/projects/pyramid/en/latest/api/testing.html?awesome
# After setUp is finished, the registry returned by the pyramid.threadlocal.get_current_request() function will be the passed (or constructed) registry until pyramid.testing.tearDown() is called (or pyramid.testing.setUp() is called again) .
pyramid.testing.setUp()
pyramid.paster.bootstrap('development.ini')
self.db_sess = db_init()
def tearDown(self):
pyramid.testing.tearDown()
def test_1(self):
"""1回目は get_current_registry().settings がうまくとれている"""
d = pyramid.threadlocal.get_current_registry().settings
sys.stderr.write("\033[0;31m*** debug: d=%r\033[0m\n" % (d, ))
self.assertNotEqual(None, d)
def test_2(self):
"""しかし2回目はpyramid.testing.tearDown()が呼ばれたあとなのでうまくとれない?"""
d = pyramid.threadlocal.get_current_registry().settings
sys.stderr.write("\033[0;31m*** debug: d=%r\033[0m\n" % (d, ))
self.assertNotEqual(None, d)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment