I am a newbie to TurboGears and to Python. Been studying TurboGears for a couple of weeks, and when trying to implement testing, I'm having some problems/questions!
1. I've modified the sample test scripts (in the '/tests' directory), then run nosetests. I kept getting the error: ProgrammingError: (1146, "Table 'research_test.sandboxes' doesn't exist")
I'm using MySQL on Ubuntu Linux. I have defined different databases for development and testing (research and research_test respectively.
I finally manually created the sandboxes table. Re-running nosetests reported other errors in my code which I was able to correct. I ran nosetests again, and the sandboxes table was again missing!!
My model defines a single table with 8 fields. 'tg-admin sql create' successfully creates this table in the development database, but I can't find a way to create the table in the testing database!!?!
Is there a way to have the model table(s) created (either programmaticaly or some tg-admin command) AND/OR prevent the testing tables from being deleted when running nosetests??
The setup and tearDown methods on testutils.DBTest automatically create and drop the database schema per testcase. For this to work you need to set the model attribute of the class to reference your model module. It's common to use an SQLite 'in memory' database for unittests to speed-up the peformance.
Joost Moesker wrote: > The setup and tearDown methods on testutils.DBTest automatically > create and drop the database schema per testcase.
DBTest is SQLObject-specific
In my case (with sqlalchemy) I cannot drop and recreate the schema for each test because it would take too much time -- the schema is postgres-specific, autoloaded, and has triggers, bells and whistles.
> class DBTestCase(unittest.TestCase): > transaction = None
> def setUp(self): > # In case of errors, we cleare here the session and > transaction, or other tests will fail > if self.transaction: > self.transaction.rollback() > self.transaction = None > session.clear()
> self.transaction = session.create_transaction()
> def tearDown(self): > # Hit the db before rollback > # This way, we catch some integrity errors, type checking from > sqlalchemy, and the like > session.flush() > self.transaction.rollback() > self.transaction = None > session.clear()
Works for me. The first session.clear() is suspicious, but I had no time to investigate yet
I don't understand what you're saying here!!??!! Can you be more specific about what needs to be done, may examples of what I may need to add to my testing code!?!! I'm not using SQLite, but MySQL.
test-models.py seems to complete OK. but test-controllers.py is where my problem seems to be. If I manually create the required table in the above database, nosetests completes successfully, but the table has been deleted!!?!!
Below are the results of my latest nosetests run; and the contents of my test-controllers.py (which seems to be what's failing):
====================================================================== ERROR: test module research.tests.test_controllers in /home/dthury/ projects/Somerville/research ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ suite.py", line 51, in run self.setUp() File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ suite.py", line 200, in setUp self.module = _import(self.moduleName, [self.path], self.conf) File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ importer.py", line 101, in _import mod = load_module(fqname, fh, filename, desc) File "/home/dthury/projects/Somerville/research/research/tests/ test_controllers.py", line 2, in ? from research.controllers import * File "/home/dthury/projects/Somerville/research/research/ controllers.py", line 26, in ? class sandboxSelectionFields(widgets.WidgetsList): File "/home/dthury/projects/Somerville/research/research/ controllers.py", line 27, in sandboxSelectionFields sandboxID = widgets.RadioButtonList(options=get_sandbox_options(), validator=validators.NotEmpty) File "/home/dthury/projects/Somerville/research/research/ controllers.py", line 21, in get_sandbox_options for sandbox in sandbox_list: File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/sresults.py", line 155, in __iter__ return iter(list(self.lazyIter())) File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/sresults.py", line 163, in lazyIter return conn.iterSelect(self) File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/dbconnection.py", line 365, in iterSelect select, keepConnection=False) File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/dbconnection.py", line 705, in __init__ self.dbconn._executeRetry(self.rawconn, self.cursor, self.query) File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/mysql/mysqlconnection.py", line 78, in _executeRetry return cursor.execute(myquery) File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute self.errorhandler(self, exc, value) File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler raise errorclass, errorvalue ProgrammingError: (1146, "Table 'research_test.sandboxes' doesn't exist")
---------------------------------------------------------------------- Ran 1 test in 1.636s
from turbogears import testutil from research.controllers import * from research.model import * import cherrypy
cherrypy.root = Root()
def test_sandbox_controller(): result = testutil.call(cherrypy.root.play) print 'play returned :', result assert result == True
def test_method(): "the index method should return a string called now" import types result = testutil.call(cherrypy.root.index) print "Index method returned:", result
def test_indextitle(): "The indexpage should have the right title" testutil.createRequest("/") print cherrypy.response.body[0] assert "<title>SNP Research System</title>" in cherrypy.response.body[0]
Thanks, Denny
On Apr 16, 4:13 pm, "Joost Moesker" <JoostMoes...@gmail.com> wrote:
> The setup and tearDown methods on testutils.DBTest automatically > create and drop the database schema per testcase. For this to work > you need to set the model attribute of the class to reference your > model module. It's common to use an SQLite 'in memory' database for > unittests to speed-up the peformance.
The following setup will DROP and CREATE your database therefore deleting all data in it before each test. You will need to populate your data base with test data in the setUp method.
> I don't understand what you're saying here!!??!! Can you be more > specific about what needs to be done, > may examples of what I may need to add to my testing code!?!! I'm not > using SQLite, but MySQL.
> test-models.py seems to complete OK. but test-controllers.py is where > my problem seems to be. > If I manually create the required table in the above database, > nosetests completes successfully, but > the table has been deleted!!?!!
> Below are the results of my latest nosetests run; and > the contents of my test-controllers.py (which seems to be what's > failing):
> ====================================================================== > ERROR: test module research.tests.test_controllers in /home/dthury/ > projects/Somerville/research > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > suite.py", line 51, in run > self.setUp() > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > suite.py", line 200, in setUp > self.module = _import(self.moduleName, [self.path], self.conf) > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > importer.py", line 101, in _import > mod = load_module(fqname, fh, filename, desc) > File "/home/dthury/projects/Somerville/research/research/tests/ > test_controllers.py", line 2, in ? > from research.controllers import * > File "/home/dthury/projects/Somerville/research/research/ > controllers.py", line 26, in ? > class sandboxSelectionFields(widgets.WidgetsList): > File "/home/dthury/projects/Somerville/research/research/ > controllers.py", line 27, in sandboxSelectionFields > sandboxID = widgets.RadioButtonList(options=get_sandbox_options(), > validator=validators.NotEmpty) > File "/home/dthury/projects/Somerville/research/research/ > controllers.py", line 21, in get_sandbox_options > for sandbox in sandbox_list: > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/sresults.py", line 155, in __iter__ > return iter(list(self.lazyIter())) > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/sresults.py", line 163, in lazyIter > return conn.iterSelect(self) > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/dbconnection.py", line 365, in iterSelect > select, keepConnection=False) > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/dbconnection.py", line 705, in __init__ > self.dbconn._executeRetry(self.rawconn, self.cursor, self.query) > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/mysql/mysqlconnection.py", line 78, in _executeRetry > return cursor.execute(myquery) > File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line > 137, in execute > self.errorhandler(self, exc, value) > File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line > 33, in defaulterrorhandler > raise errorclass, errorvalue > ProgrammingError: (1146, "Table 'research_test.sandboxes' doesn't > exist")
> ---------------------------------------------------------------------- > Ran 1 test in 1.636s
> from turbogears import testutil > from research.controllers import * > from research.model import * > import cherrypy
> cherrypy.root = Root()
> def test_sandbox_controller(): > result = testutil.call(cherrypy.root.play) > print 'play returned :', result > assert result == True
> def test_method(): > "the index method should return a string called now" > import types > result = testutil.call(cherrypy.root.index) > print "Index method returned:", result
> def test_indextitle(): > "The indexpage should have the right title" > testutil.createRequest("/") > print cherrypy.response.body[0] > assert "<title>SNP Research System</title>" in > cherrypy.response.body[0]
> Thanks, > Denny
> On Apr 16, 4:13 pm, "Joost Moesker" <JoostMoes...@gmail.com> wrote: > > The setup and tearDown methods on testutils.DBTest automatically > > create and drop the database schema per testcase. For this to work > > you need to set the model attribute of the class to reference your > > model module. It's common to use an SQLite 'in memory' database for > > unittests to speed-up the peformance.
Still no-go!!! 'nosetests' seems to fail while importing my 'research.controllers' module. It seems to want the database tables during that import. The below traceback is pointing to a function in my controllers.py which makes queries for data to be presented in a radio button list. Am I doing something fundamentally wrong??
Here's the recent traceback!
> nosetests
E. ====================================================================== ERROR: test module research.tests.test_controllers in /home/dthury/ projects/Somerville/research ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ suite.py", line 51, in run self.setUp() File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ suite.py", line 200, in setUp self.module = _import(self.moduleName, [self.path], self.conf) File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ importer.py", line 101, in _import mod = load_module(fqname, fh, filename, desc)
<--- below is from my code --->
File "/home/dthury/projects/Somerville/research/research/tests/ test_controllers.py", line 8, in ? from research.controllers import * File "/home/dthury/projects/Somerville/research/research/ controllers.py", line 27, in ? class sandboxSelectionFields(widgets.WidgetsList): File "/home/dthury/projects/Somerville/research/research/ controllers.py", line 28, in sandboxSelectionFields sandboxID = widgets.RadioButtonList(options=get_sandbox_options(), validator=validators.NotEmpty) File "/home/dthury/projects/Somerville/research/research/ controllers.py", line 22, in get_sandbox_options for sandbox in sandbox_list:
<--- The above line is referring to a line of code where I'm iterating over the results of a db query But this is done before the setUp function is run to create/ load my test database!! --->
File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/sresults.py", line 155, in __iter__ return iter(list(self.lazyIter())) File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/sresults.py", line 163, in lazyIter return conn.iterSelect(self) File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/dbconnection.py", line 365, in iterSelect select, keepConnection=False) File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/dbconnection.py", line 705, in __init__ self.dbconn._executeRetry(self.rawconn, self.cursor, self.query) File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ sqlobject/mysql/mysqlconnection.py", line 78, in _executeRetry return cursor.execute(myquery) File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute self.errorhandler(self, exc, value) File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler raise errorclass, errorvalue ProgrammingError: (1146, "Table 'research_test.sandboxes' doesn't exist")
---------------------------------------------------------------------- Ran 1 test in 1.516s
> The following setup will DROP and CREATE your database therefore > deleting all data in it before each test. You will need to populate > your data base with test data in the setUp method.
> > I don't understand what you're saying here!!??!! Can you be more > > specific about what needs to be done, > > may examples of what I may need to add to my testing code!?!! I'm not > > using SQLite, but MySQL.
> > test-models.py seems to complete OK. but test-controllers.py is where > > my problem seems to be. > > If I manually create the required table in the above database, > > nosetests completes successfully, but > > the table has been deleted!!?!!
> > Below are the results of my latest nosetests run; and > > the contents of my test-controllers.py (which seems to be what's > > failing):
> > ====================================================================== > > ERROR: test module research.tests.test_controllers in /home/dthury/ > > projects/Somerville/research > > ---------------------------------------------------------------------- > > Traceback (most recent call last): > > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > > suite.py", line 51, in run > > self.setUp() > > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > > suite.py", line 200, in setUp > > self.module = _import(self.moduleName, [self.path], self.conf) > > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > > importer.py", line 101, in _import > > mod = load_module(fqname, fh, filename, desc) > > File "/home/dthury/projects/Somerville/research/research/tests/ > > test_controllers.py", line 2, in ? > > from research.controllers import * > > File "/home/dthury/projects/Somerville/research/research/ > > controllers.py", line 26, in ? > > class sandboxSelectionFields(widgets.WidgetsList): > > File "/home/dthury/projects/Somerville/research/research/ > > controllers.py", line 27, in sandboxSelectionFields > > sandboxID = widgets.RadioButtonList(options=get_sandbox_options(), > > validator=validators.NotEmpty) > > File "/home/dthury/projects/Somerville/research/research/ > > controllers.py", line 21, in get_sandbox_options > > for sandbox in sandbox_list: > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > sqlobject/sresults.py", line 155, in __iter__ > > return iter(list(self.lazyIter())) > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > sqlobject/sresults.py", line 163, in lazyIter > > return conn.iterSelect(self) > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > sqlobject/dbconnection.py", line 365, in iterSelect > > select, keepConnection=False) > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > sqlobject/dbconnection.py", line 705, in __init__ > > self.dbconn._executeRetry(self.rawconn, self.cursor, self.query) > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > sqlobject/mysql/mysqlconnection.py", line 78, in _executeRetry > > return cursor.execute(myquery) > > File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line > > 137, in execute > > self.errorhandler(self, exc, value) > > File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line > > 33, in defaulterrorhandler > > raise errorclass, errorvalue > > ProgrammingError: (1146, "Table 'research_test.sandboxes' doesn't > > exist")
> > ---------------------------------------------------------------------- > > Ran 1 test in 1.636s
> > from turbogears import testutil > > from research.controllers import * > > from research.model import * > > import cherrypy
> > cherrypy.root = Root()
> > def test_sandbox_controller(): > > result = testutil.call(cherrypy.root.play) > > print 'play returned :', result > > assert result == True
> > def test_method(): > > "the index method should return a string called now" > > import types > > result = testutil.call(cherrypy.root.index) > > print "Index method returned:", result
> > def test_indextitle(): > > "The indexpage should have the right title" > > testutil.createRequest("/") > > print cherrypy.response.body[0] > > assert "<title>SNP Research System</title>" in > > cherrypy.response.body[0]
> > Thanks, > > Denny
> > On Apr 16, 4:13 pm, "Joost Moesker" <JoostMoes...@gmail.com> wrote: > > > The setup and tearDown methods on testutils.DBTest automatically > > > create and drop the database schema per testcase. For this to work > > > you need to set the model attribute of the class to reference your > > > model module. It's common to use an SQLite 'in memory' database for > > > unittests to speed-up the peformance.
> Still no-go!!! 'nosetests' seems to fail while importing my > 'research.controllers' module. It seems to want the database tables > during that import. The below traceback is pointing to a function in > my controllers.py which makes queries for data to be presented in a > radio button list. Am I doing something fundamentally wrong??
> Here's the recent traceback!
> > nosetests > E. > ====================================================================== > ERROR: test module research.tests.test_controllers in /home/dthury/ > projects/Somerville/research > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > suite.py", line 51, in run > self.setUp() > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > suite.py", line 200, in setUp > self.module = _import(self.moduleName, [self.path], self.conf) > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > importer.py", line 101, in _import > mod = load_module(fqname, fh, filename, desc)
> <--- below is from my code --->
> File "/home/dthury/projects/Somerville/research/research/tests/ > test_controllers.py", line 8, in ? > from research.controllers import * > File "/home/dthury/projects/Somerville/research/research/ > controllers.py", line 27, in ? > class sandboxSelectionFields(widgets.WidgetsList): > File "/home/dthury/projects/Somerville/research/research/ > controllers.py", line 28, in sandboxSelectionFields > sandboxID = widgets.RadioButtonList(options=get_sandbox_options(), > validator=validators.NotEmpty) > File "/home/dthury/projects/Somerville/research/research/ > controllers.py", line 22, in get_sandbox_options > for sandbox in sandbox_list:
> <--- The above line is referring to a line of code where I'm > iterating over the results of a db query > But this is done before the setUp function is run to create/ > load my test database!! --->
> File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/sresults.py", line 155, in __iter__ > return iter(list(self.lazyIter())) > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/sresults.py", line 163, in lazyIter > return conn.iterSelect(self) > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/dbconnection.py", line 365, in iterSelect > select, keepConnection=False) > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/dbconnection.py", line 705, in __init__ > self.dbconn._executeRetry(self.rawconn, self.cursor, self.query) > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > sqlobject/mysql/mysqlconnection.py", line 78, in _executeRetry > return cursor.execute(myquery) > File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line > 137, in execute > self.errorhandler(self, exc, value) > File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line > 33, in defaulterrorhandler > raise errorclass, errorvalue > ProgrammingError: (1146, "Table 'research_test.sandboxes' doesn't > exist")
> ---------------------------------------------------------------------- > Ran 1 test in 1.516s
> On Apr 17, 8:35 pm, "Ian Wilson" <vengfulsquir...@gmail.com> wrote: > > The following setup will DROP and CREATE your database therefore > > deleting all data in it before each test. You will need to populate > > your data base with test data in the setUp method.
> > > I don't understand what you're saying here!!??!! Can you be more > > > specific about what needs to be done, > > > may examples of what I may need to add to my testing code!?!! I'm not > > > using SQLite, but MySQL.
> > > test-models.py seems to complete OK. but test-controllers.py is where > > > my problem seems to be. > > > If I manually create the required table in the above database, > > > nosetests completes successfully, but > > > the table has been deleted!!?!!
> > > Below are the results of my latest nosetests run; and > > > the contents of my test-controllers.py (which seems to be what's > > > failing):
> > > ====================================================================== > > > ERROR: test module research.tests.test_controllers in /home/dthury/ > > > projects/Somerville/research > > > ---------------------------------------------------------------------- > > > Traceback (most recent call last): > > > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > > > suite.py", line 51, in run > > > self.setUp() > > > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > > > suite.py", line 200, in setUp > > > self.module = _import(self.moduleName, [self.path], self.conf) > > > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > > > importer.py", line 101, in _import > > > mod = load_module(fqname, fh, filename, desc) > > > File "/home/dthury/projects/Somerville/research/research/tests/ > > > test_controllers.py", line 2, in ? > > > from research.controllers import * > > > File "/home/dthury/projects/Somerville/research/research/ > > > controllers.py", line 26, in ? > > > class sandboxSelectionFields(widgets.WidgetsList): > > > File "/home/dthury/projects/Somerville/research/research/ > > > controllers.py", line 27, in sandboxSelectionFields > > > sandboxID = widgets.RadioButtonList(options=get_sandbox_options(), > > > validator=validators.NotEmpty) > > > File "/home/dthury/projects/Somerville/research/research/ > > > controllers.py", line 21, in get_sandbox_options > > > for sandbox in sandbox_list: > > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > > sqlobject/sresults.py", line 155, in __iter__ > > > return iter(list(self.lazyIter())) > > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > > sqlobject/sresults.py", line 163, in lazyIter > > > return conn.iterSelect(self) > > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > > sqlobject/dbconnection.py", line 365, in iterSelect > > > select, keepConnection=False) > > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > > sqlobject/dbconnection.py", line 705, in __init__ > > > self.dbconn._executeRetry(self.rawconn, self.cursor, self.query) > > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > > sqlobject/mysql/mysqlconnection.py", line 78, in _executeRetry > > > return cursor.execute(myquery) > > > File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line > > > 137, in execute > > > self.errorhandler(self, exc, value) > > > File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line > > > 33, in defaulterrorhandler > > > raise errorclass, errorvalue > > > ProgrammingError: (1146, "Table 'research_test.sandboxes' doesn't > > > exist")
> > > ---------------------------------------------------------------------- > > > Ran 1 test in 1.636s
> > > from turbogears import testutil > > > from research.controllers import * > > > from research.model import * > > > import cherrypy
> > > cherrypy.root = Root()
> > > def test_sandbox_controller(): > > > result = testutil.call(cherrypy.root.play) > > > print 'play returned :', result > > > assert result == True
> > > def test_method(): > > > "the index method should return a string called now" > > > import types > > > result = testutil.call(cherrypy.root.index) > > > print "Index method returned:", result
> > > def test_indextitle(): > > > "The indexpage should have the right title" > > > testutil.createRequest("/") > > > print cherrypy.response.body[0] > > > assert "<title>SNP Research System</title>" in > > > cherrypy.response.body[0]
> > > Thanks, > > > Denny
> > > On Apr 16, 4:13 pm, "Joost Moesker" <JoostMoes...@gmail.com> wrote: > > > > The setup and tearDown methods on testutils.DBTest automatically > > > > create and drop the database schema per testcase. For this to work > > > > you need to set the model attribute of the class to reference your > > > > model module. It's common to use an SQLite 'in memory' database for > > > > unittests to speed-up the peformance.
> > Still no-go!!! 'nosetests' seems to fail while importing my > > 'research.controllers' module. It seems to want the database tables > > during that import. The below traceback is pointing to a function in > > my controllers.py which makes queries for data to be presented in a > > radio button list. Am I doing something fundamentally wrong??
> > Here's the recent traceback!
> > > nosetests > > E. > > ====================================================================== > > ERROR: test module research.tests.test_controllers in /home/dthury/ > > projects/Somerville/research > > ---------------------------------------------------------------------- > > Traceback (most recent call last): > > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > > suite.py", line 51, in run > > self.setUp() > > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > > suite.py", line 200, in setUp > > self.module = _import(self.moduleName, [self.path], self.conf) > > File "/usr/lib/python2.4/site-packages/nose-0.9.2-py2.4.egg/nose/ > > importer.py", line 101, in _import > > mod = load_module(fqname, fh, filename, desc)
> > <--- below is from my code --->
> > File "/home/dthury/projects/Somerville/research/research/tests/ > > test_controllers.py", line 8, in ? > > from research.controllers import * > > File "/home/dthury/projects/Somerville/research/research/ > > controllers.py", line 27, in ? > > class sandboxSelectionFields(widgets.WidgetsList): > > File "/home/dthury/projects/Somerville/research/research/ > > controllers.py", line 28, in sandboxSelectionFields > > sandboxID = widgets.RadioButtonList(options=get_sandbox_options(), > > validator=validators.NotEmpty) > > File "/home/dthury/projects/Somerville/research/research/ > > controllers.py", line 22, in get_sandbox_options > > for sandbox in sandbox_list:
> > <--- The above line is referring to a line of code where I'm > > iterating over the results of a db query > > But this is done before the setUp function is run to create/ > > load my test database!! --->
> > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > sqlobject/sresults.py", line 155, in __iter__ > > return iter(list(self.lazyIter())) > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > sqlobject/sresults.py", line 163, in lazyIter > > return conn.iterSelect(self) > > File "/usr/lib/python2.4/site-packages/SQLObject-0.7.4-py2.4.egg/ > > sqlobject/dbconnection.py", line 365, in i