Test Test
Test Test

Reputation: 31

jython unit testing

May I know why this test fails even though the function actually throws the exception?

def testDateCreation(self):
   self.assertRaises(ValueError, datetime.date(2013, 2, 29))

Upvotes: 2

Views: 494

Answers (1)

sloth
sloth

Reputation: 101052

You either have to use assertRaises as context manager (if running python 2.7):

with self.assertRaises(ValueError):
    datetime.date(2013, 2, 29)

or provide a function which assertRaises can call:

self.assertRaises(ValueError, lambda: datetime.date(2013, 2, 29))

Otherwise, the exception is raised before assertRaises is called, and thus can't be handled.

Upvotes: 2

Related Questions