[Tutor] unittesting and nested functions

Yigal Duppen yduppen at xs4all.nl
Tue Dec 21 12:43:18 CET 2004


On Monday 20 December 2004 21:16, Christian Meesters wrote:
> 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'))

Apart from the correct answer given by Kent Johnson, I personally prefer to 
code such tests like this:

def test_stuff(self):
	try:
		my_function("silly argument")
		self.fail("my_function succeeded, even though I passed silly argument")
	except IOError:
		# and this was expected
		pass

The reason for this is easy: I always make stupid mistakes in calls to 
failUnlessRaises; and I find this approach much more readable. 

I hope this helps,
Yigal Duppen


More information about the Tutor mailing list