unit testing and committing transactions
I'm trying to start introducing some unit tests to a large body of of code. The project is somewhat interdependent - I have to create a Zope.app() in order to instantiate a lot of what I'm testing. Because of this, I'm lowering my expectations, aiming for a regression test suite, and hoping that subsets of it can be used in a more unit testing manner. My current problem is that transactions will be committed during the instantiation of some of the classes that I'm trying to test, making it hard for me to get a clean slate each time. My solution is to create a new DemoStorage to be loaded from custom_zodb in the Testing module, and calling Zope.App.startup.startup() manually (since it won't be called more than once by default). This works so far, but it's inefficient, and requires some slight knowledge of the storage being used. Is this a good idea? Is there a better way? The fragment below illustrates what I'm doing. I'm commit()ing manually as a demonstration, which would cause the second setUp() to complain about the existing object without a new storage being made. def makeNewDB(): """ Overwrite the custom_zodb DemoStorage with a new one, then call startup to make a new DB. This is needed when we want a clean slate after a transaction has been committed. """ m = imp.find_module('custom_zodb', [TESTING_HOME]) m = imp.load_module('Zope.custom_zodb', m[0], m[1], m[2]) m.Storage = DemoStorage(quota=(1<<20)) # call the app startup manually - it won't be called by Zope.startup() # more than once. This only works after a call to Zope.app(). Zope.App.startup.startup() class testFoo(unittest.TestCase): def setUp(self): self.app = Zope.app() makeNewDB() self.app._setObject('foo', OFS.ObjectManager.ObjectManager()) get_transaction().commit() def testPass(self): self.assertEqual(1,1) def testPass1(self): self.assertEqual(1,1) -- Karl Anderson kra@monkey.org http://monkey.org/~kra/
On Dienstag, Mär 23, 2004, at 00:03 Europe/Vienna, Karl Anderson wrote:
def makeNewDB(): """ Overwrite the custom_zodb DemoStorage with a new one, then call startup to make a new DB. This is needed when we want a clean slate after a transaction has been committed. """ m = imp.find_module('custom_zodb', [TESTING_HOME]) m = imp.load_module('Zope.custom_zodb', m[0], m[1], m[2]) m.Storage = DemoStorage(quota=(1<<20)) # call the app startup manually - it won't be called by Zope.startup() # more than once. This only works after a call to Zope.app(). Zope.App.startup.startup()
You can replace the above with 'import Testing'. This modifies the configuration and Zope.app() will do the right thing (tm). Neat, huh?
class testFoo(unittest.TestCase): def setUp(self): self.app = Zope.app() self.app._setObject('foo', OFS.ObjectManager.ObjectManager()) get_transaction().commit()
As a simple hack you can define a tearDown() method like so: def tearDown(self): self.app._delObject('foo') get_transaction().commit() self.app._p_jar.close()
def testPass(self): self.assertEqual(1,1)
def testPass1(self): self.assertEqual(1,1)
You may also be interested in the ZopeTestCase package which takes care of all these things for you: <http://zope.org/Members/shh/ZopeTestCase> Stefan -- The time has come to start talking about whether the emperor is as well dressed as we are supposed to think he is. /Pete McBreen/
Karl Anderson wrote at 2004-3-22 15:03 -0800:
... My solution is to create a new DemoStorage to be loaded from custom_zodb in the Testing module, and calling Zope.App.startup.startup() manually (since it won't be called more than once by default).
This works so far, but it's inefficient, and requires some slight knowledge of the storage being used.
The inefficient thing is loading all products. You can selectively load products and speed up things considerably. -- Dieter
participants (3)
-
Dieter Maurer -
Karl Anderson -
Stefan H. Holek