assertRaises() in django unittest framework doesn't catch exceptions

640 views
Skip to first unread message

Karantir

unread,
Mar 27, 2008, 10:42:53 AM3/27/08
to Django users
Hi,
I use unittest framework and recently i've faced strange issue. I use
asserRaises() method to ensure that my model methods raises
appropriate exceptions. And methods raises, but asserRaises() doesn't
catch them, therefore i see an error with following traceback

<code>
======================================================================
ERROR: testCreate (logrus.documents.tests.testDocument)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\webservers\home\django\logrus\..\logrus\documents
\tests.py", line 59, in testCreate
self.assertRaises(RevisionNotStarted,
self.documents[0].fix_revision())
File "D:\webservers\home\django\logrus\..\logrus\documents
\models.py", line 109, in fix_revision
raise RevisionNotStarted
RevisionNotStarted
</code>

Karantir

unread,
Mar 27, 2008, 10:49:55 AM3/27/08
to Django users
And here is a bit of my testcase
[code]
self.assertRaises(RevisionNotStarted,
self.documents[0].fix_revision())
[/code]

Marty Alchin

unread,
Mar 28, 2008, 2:39:56 PM3/28/08
to django...@googlegroups.com

Try removing the extra parentheses from your test case. assertRaises
expects to get a callable, which it can call within its own try/catch
block. You're instead calling the fix_revision() method before
assertRaises even has a chance to see the function. There's no way it
could catch the error in this case, so the error you reported is
natural.

[code]
self.assertRaises(RevisionNotStarted,
self.documents[0].fix_revision)
[/code]

-Gul

Reply all
Reply to author
Forward
0 new messages