[Tutor] unittesting and nested functions

Christian Meesters cmeesters at ucdavis.edu
Mon Dec 20 21:16:25 CET 2004


Hi

I've written some unittests using the unittest module for my 
'DNA-class'. This class can cope with almost every input, but with no 
'numbers' whatsoever. Well, one of the tests is:

class TestFunctions(unittest.TestCase):
     ...
     def test__init__(self):
         """testing whether __init__ will fail if nonsense data are 
passed for initialization"""
         self.failUnlessRaises(IOError,DNA('1 ATG'))
		
Now, if 'nonsense data' are passed to the testing function I expect an 
IOError to be raised. And this happens indeed. Still this test fails 
(actually it doesn't: only an error occurs), but why?

This is the Traceback I get:

======================================================================
ERROR: testing whether __init__ will fail if nonsense data are passed 
for initialization
----------------------------------------------------------------------
Traceback (most recent call last):
   File "src/DNA.py", line 862, in test__init__
     self.failUnlessRaises(IOError,DNA('1 ATG'))
   File "src/DNA.py", line 100, in __init__
     raise IOError
IOError

----------------------------------------------------------------------
Ran 35 tests in 0.485s

FAILED (errors=1)

According to the documentation everything should be just fine:
----------
5.3.5 TestCase Objects
...
failUnlessRaises(exception, callable, ...)
  Test that an exception is raised when callable is called with  any 
positional or keyword arguments that are also passed to assertRaises(). 
The test passes if exception is  raised, is an error if another 
exception is raised, or fails if no  exception is raised. To catch any 
of a group of exceptions, a tuple  containing the exception classes may 
be passed as exception.
----------

So, any idea, where my mistake is?

(Before I get comments about having the unittest in the same file as 
the file to be tested: I'll change that! ;-) )

Thanks a lot in advance.
Cheers
Christian



More information about the Tutor mailing list