Re: [Pyunit-interest] test case independence
Brought to you by:
purcell
From: Steve P. <ste...@ya...> - 2002-04-15 07:01:40
|
Hi J.D., Response in-line below.... J.D. Hollis wrote: > class SeedTestCase(unittest.TestCase): > "Test cases for class Seed" > > def setUp(self): > self.seed = db.Seed('name') > > [snip] > > def testGetValueFailure(self): > """Tests whether Seed.getValue() raises an exception when it > can't find key""" > self.assertRaises(db.NoSuchKeyError, self.seed.getValue, 'key') > > def testSuite(): > return unittest.makeSuite(SeedTestCase, 'test') > > I'm invoking the test runner in the python shell using > 'runner.run(dbtest.testSuite())' and testGetValueFailure() is failing: > > ====================================================================== > FAIL: Tests whether Seed.getValue() raises an exception when it can't > find key > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "C:\Documents and Settings\J.D. Hollis\My > Documents\garden\plant\dbtest.py", line 41, in testGetValueFailure > self.assertRaises(db.NoSuchKeyError, self.seed.getValue, 'key') > File "C:\Python22\lib\unittest.py", line 279, in failUnlessRaises > raise self.failureException, excName > AssertionError: NoSuchKeyError Well, 'self.seed' will certainly be created freshly by the 'setUp()' method just before 'testGetValueFailure()' is run, so I think that is not the problem, assuming 'db.Seed()' works correctly. To double-check, I think that if you add the "self.seed = db.Seed('name')" line to the 'testGetValueFailure()' method, the same thing will happen. Now, when 'assertRaises()' fails, it prints the name of the unexpected exception it caught. In your case, it prints 'NoSuchKeyError', so my guess is that for some reason 'db.NoSuchKeyError' in the test script is not equal to the 'NoSuchKeyError' in the 'db' module. This has caught me out sometimes in Python, but I forget right now how it comes about. I remember having to change code in 'unittest.py' for exactly that reason: This: if type(obj) == types.ModuleType: return self.loadTestsFromModule(obj) elif type(obj) == types.ClassType and issubclass(obj, TestCase): return self.loadTestsFromTestCase(obj) Became this: import unittest if type(obj) == types.ModuleType: return self.loadTestsFromModule(obj) elif type(obj) == types.ClassType and issubclass(obj, unittest.TestCase): return self.loadTestsFromTestCase(obj) Hope that helps somewhat. Best wishes, -Steve -- Steve Purcell http://advogato.org/person/purcell |