assertRaises doesnt catch custom error

1,466 views
Skip to first unread message

pault

unread,
Feb 14, 2009, 2:28:03 PM2/14/09
to Django users
I have defined a custom error but if I test if custom error gets
raised it fails if I test for Exception the test passes

class CustomError(Exception):
"""
This exception is my custom error
"""

class Company(models.Model):
name = models.CharField(max_length=200)

def test_error(self):
raise CustomError('hello')


and in my tests.py:

import unittest
from api.models import
Company,Customer,Employee,Location,Product,ProductCategory,AllreadyPayedError,CustomError

#class AllreadyPayedException(Exception): pass

class CompanyTestCase(unittest.TestCase):
def setUp(self):
self.company = Company.objects.create(name="lizto")

def test2(self):
self.assertRaises(CustomError, self.company.test_error)


======================================================================
ERROR: test2 (lizto.api.tests.CompanyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/......./tests.py", line 27, in test2
self.assertRaises(CustomError, self.company.test_error)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/unittest.py", line 320, in failUnlessRaises
callableObj(*args, **kwargs)
File "/Users/....../models.py", line 17, in test_error
raise CustomError('hello')
CustomError: hello

----------------------------------------------------------------------
Ran 18 tests in 1.122s


Anybody an idea what I should do to test if CustomError gets raised

Malcolm Tredinnick

unread,
Feb 14, 2009, 9:31:11 PM2/14/09
to django...@googlegroups.com
On Sat, 2009-02-14 at 11:28 -0800, pault wrote:
> I have defined a custom error but if I test if custom error gets
> raised it fails if I test for Exception the test passes
>
> class CustomError(Exception):
> """
> This exception is my custom error
> """
>
> class Company(models.Model):
> name = models.CharField(max_length=200)
>
> def test_error(self):
> raise CustomError('hello')
>
>
> and in my tests.py:
>
> import unittest
> from api.models import
> Company,Customer,Employee,Location,Product,ProductCategory,AllreadyPayedError,CustomError
>
> #class AllreadyPayedException(Exception): pass

You mean AlreadyPaidException. Trust me on this. :-)

>
> class CompanyTestCase(unittest.TestCase):
> def setUp(self):
> self.company = Company.objects.create(name="lizto")
>
> def test2(self):
> self.assertRaises(CustomError, self.company.test_error)
>
>
> ======================================================================
> ERROR: test2 (lizto.api.tests.CompanyTestCase)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
> File "/Users/......./tests.py", line 27, in test2
> self.assertRaises(CustomError, self.company.test_error)
> File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/
> python2.5/unittest.py", line 320, in failUnlessRaises
> callableObj(*args, **kwargs)
> File "/Users/....../models.py", line 17, in test_error
> raise CustomError('hello')
> CustomError: hello

Using the code you provide (after removing the imports that don't exist
in the code you provide), this works perfectly for me. So there's
something special about your setup.

If I were you, I'd start from the simple example you show. Create an app
that only contains that models.py and that tests.py and fix the import
line to remove everything except Company and CustomError. That's what
passes for me.

Regards,
Malcolm

pault

unread,
Feb 15, 2009, 9:46:12 AM2/15/09
to Django users
Malcom many thanks for your reply I created a new project and app to
with only one model and one test.
It still fails I know I am missing something but I cant figure out
what that is.

I tested this on both mac OSx10.5 and vista (both python 2.5.1 and
django version 1.02)

Here are my models.py and my tests.py
http://dpaste.com/120843/

The only other thing I changed is add myproj.myapp to my
INSTALLED_APPS

Karen Tracey

unread,
Feb 15, 2009, 10:18:32 AM2/15/09
to django...@googlegroups.com
On Sun, Feb 15, 2009 at 9:46 AM, pault <pau...@gmail.com> wrote:

Malcom many thanks for your reply I created a new project and app to
with only one model and one test.
It still fails I know I am missing something but I cant figure out
what that is.

I tested this on both mac OSx10.5 and vista (both python 2.5.1 and
django version 1.02)

Here are my models.py and my tests.py
http://dpaste.com/120843/

The only other thing I changed is add myproj.myapp to my
INSTALLED_APPS

Specify just myapp, not myproj.myapp, in INSTALLED_APPS.

Alternatively the error can be fixed by changing:

from myapp.models import Company,CustomError

to:

from myproj.myapp.models import Company,CustomError

in your tests.py, but that isn't what I would do.  The fact that it works to fix the error just points towards the fact that you need to be consistent in whether it's myproj.myapp.models or just myapp.models you are using.  I think it's generally easier (and more re-usable when you want to move things from project to project) to consistently omit the project part in the imports, so I'd go with the first option.

Karen

pault

unread,
Feb 15, 2009, 10:42:41 AM2/15/09
to Django users
Thanks Karen, it now works as expected ! .

In the tutorial http://docs.djangoproject.com/en/dev/intro/tutorial01/#intro-tutorial01
they use the proj in imports but I think the your first option is
better as it is more pluggable in other projects that way.
I wonder if there are any pros to do it like that as your option looks
"better practice"?

Thanks again for your help.

regards Paul



On Feb 15, 4:18 pm, Karen Tracey <kmtra...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages