how to test for exceptions with nose?

45 views
Skip to first unread message

Jorge Vargas

unread,
Aug 4, 2006, 1:46:23 AM8/4/06
to turbo...@googlegroups.com
Hi

I have been reading about nose and I find it great.

I have been looking into TG's test and all exceptions related are like this, so say the exception name in the string but what if I must be sure that I get exception type X
    try:
w.name = "foo"
assert False, "should have gotten an exception"
except ValueError:
pass

I have this, but it will actually fail all the time, and since we don't have python 2.6 we can't use the try/catch/finally, so any suggestions on how to test for method foo to raise exception bar and fail the test if that doesn't happen?

    def test_create(self):
        try:
            source.create ()
        except NotImplementedError:
            assert True
        assert False

Stephen Thorne

unread,
Aug 4, 2006, 2:00:37 AM8/4/06
to turbo...@googlegroups.com

py.test (from codespeak.net's svn) has py.test.raises.

i.e.

in test_simple.py::

import py.test

def inverse(x):
return 1/x

def test_half():
assert inverse(2.0) == 0.5

def test_raises():
py.test.raises(ZeroDivisionError, inverse, 0.0)

def test_raises_string():
py.test.raises(ZeroDivisionError, "1.0/0.0")

Surely nose has a similar feature?
--
Regards,
Stephen Thorne
Development Engineer


Scanned by the NetBox from NetBox Blue
(http://netboxblue.com/)

Kevin Dangoor

unread,
Aug 4, 2006, 7:19:57 AM8/4/06
to turbo...@googlegroups.com
On Aug 4, 2006, at 1:46 AM, Jorge Vargas wrote:

Hi

I have been reading about nose and I find it great.

I have been looking into TG's test and all exceptions related are like this, so say the exception name in the string but what if I must be sure that I get exception type X
    try:
w.name = "foo"
assert False, "should have gotten an exception"
except ValueError:
pass

I have this, but it will actually fail all the time,

It fails all the time because you can't get the exception to raise?

What you have above is generally what I do, but if I'm testing for an exception there's usually a way to make the exception come up.

Alberto Valverde

unread,
Aug 4, 2006, 8:15:04 AM8/4/06
to turbo...@googlegroups.com
Hi, if using a unittest.TestCase subclass for your tests (like TG's DBTest) you can use the failUnlessRaises method.

class TestMyApp(TestCase):
def test_int(self):
""" test that int("a") raises a ValueError """ 
self.failUnlessRaises(ValueError, int, "a")
# As in your example:
def test_create(self):
self.failUnlessRaises(NotImplementedError, source.create)

HTH,
Alberto

Jorge Vargas

unread,
Aug 4, 2006, 9:10:51 PM8/4/06
to turbo...@googlegroups.com
On 8/4/06, Kevin Dangoor <dan...@gmail.com> wrote:

On Aug 4, 2006, at 1:46 AM, Jorge Vargas wrote:

Hi

I have been reading about nose and I find it great.

I have been looking into TG's test and all exceptions related are like this, so say the exception name in the string but what if I must be sure that I get exception type X
    try:
w.name = "foo"
assert False, "should have gotten an exception"
except ValueError:
pass

I have this, but it will actually fail all the time,

It fails all the time because you can't get the exception to raise?

sorry I was refering to the second code, the one that has
except <someException>:
    assert True
asser False

What you have above is generally what I do, but if I'm testing for an exception there's usually a way to make the exception come up.

yes but you can only assert that a exception was raised not exception type X w.name="foo" could have given anything other then what I want it to raise. and the test will still "pass"



Jorge Vargas

unread,
Aug 4, 2006, 9:16:25 PM8/4/06
to turbo...@googlegroups.com

I saw a couple of examples like that but I wanted to use nose-style tests for everything. I guess I'll have to use plain old TestCase classes.


maybe nose should implemente a set of module level functions that mimic the TestCase methods, I'll look around their trac to open a ticket/submit a patch.


but for now I'll use your approach alberto, thanks.

Kevin Dangoor

unread,
Aug 4, 2006, 11:19:56 PM8/4/06
to turbo...@googlegroups.com
I'm not sure what you mean. That except clause up there will only handle ValueErrors. Anything else that comes up will "error" the test. (Not a failure, mind you, but it still wouldn't pass..)

You could also use a boolean if you want.

got_exception = False
try:
    do_something()
except NotImplementedError:
    got_exception = True
assert got_exception

Kevin



Jorge Vargas

unread,
Aug 5, 2006, 12:19:06 AM8/5/06
to turbo...@googlegroups.com
On 8/4/06, Kevin Dangoor <dan...@gmail.com> wrote:

I'm not sure what you mean. That except clause up there will only handle ValueErrors. Anything else that comes up will "error" the test. (Not a failure, mind you, but it still wouldn't pass..)

because even if it goes in the last assert will make it always fail
    try:
        raise NotImplementedError
    except NotImplementedError:
        assert True,"True passes"
    assert False,"but here it crashes"

>python -u "assertTest.py"
Traceback (most recent call last):
  File "assertTest.py", line 5, in ?
    assert False,"but here it crashes"
AssertionError: but here it crashes
>Exit code: 1


You could also use a boolean if you want.

got_exception = False
try:
    do_something()
except NotImplementedError:
    got_exception = True
assert got_exception

now that one does pass, but then if you have more then one exception to catch, they you will have to print out the exception name or something. I believe for now using Alberto's idea is better. although I posted a ticket and I got the correct answer. http://nose.python-hosting.com/ticket/80

import nose.tools

@nose.tools.raises(NotImplementedError)
def test_something():


...

Jorge Vargas

unread,
Aug 5, 2006, 2:36:58 AM8/5/06
to turbo...@googlegroups.com

Ok I just browser their trac and this feature was added in rev 62 so it's only on nose 0.9.0 and above.

that's already on pypi so easy_install -U nose works.

Can we get this upgrade on the next TG alpha?


Michele Cella

unread,
Aug 5, 2006, 5:41:50 AM8/5/06
to TurboGears
Jorge Vargas wrote:
> On 8/4/06, Kevin Dangoor <dan...@gmail.com> wrote:
> >
> >
> > I'm not sure what you mean. That except clause up there will only handle
> > ValueErrors. Anything else that comes up will "error" the test. (Not a
> > failure, mind you, but it still wouldn't pass..)
> >
>
> because even if it goes in the last assert will make it always fail
> try:
> raise NotImplementedError
> except NotImplementedError:
> assert True,"True passes"
> assert False,"but here it crashes"
>
> >python -u "assertTest.py"
> Traceback (most recent call last):
> File "assertTest.py", line 5, in ?
> assert False,"but here it crashes"
> AssertionError: but here it crashes
> >Exit code: 1
>

I'm not sure if I'm understanding what you're really trying to do,
anyway:

try:
raise NotImplementedError
except NotImplementedError:
assert True,"True passes"

else:


assert False,"but here it crashes"

> python -u assertTest.py

Ciao
Michele

Jorge Vargas

unread,
Aug 5, 2006, 3:25:15 PM8/5/06
to turbo...@googlegroups.com
On 8/5/06, Michele Cella <michel...@gmail.com> wrote:

Jorge Vargas wrote:
> On 8/4/06, Kevin Dangoor <dan...@gmail.com> wrote:
> >
> >
> > I'm not sure what you mean. That except clause up there will only handle
> > ValueErrors. Anything else that comes up will "error" the test. (Not a
> > failure, mind you, but it still wouldn't pass..)
> >
>
> because even if it goes in the last assert will make it always fail
>     try:
>         raise NotImplementedError
>     except NotImplementedError:
>         assert True,"True passes"
>     assert False,"but here it crashes"
>
> >python -u " assertTest.py"
> Traceback (most recent call last):
>   File "assertTest.py", line 5, in ?
>     assert False,"but here it crashes"
> AssertionError: but here it crashes
> >Exit code: 1
>

I'm not sure if I'm understanding what you're really trying to do,
anyway:

just proving that the code will fail.

try:
    raise NotImplementedError
except NotImplementedError:
    assert True,"True passes"
else:
    assert False,"but here it crashes"

> python -u assertTest.py

that should work, although I like the more explicit use of TestCase.failUnlessRaises and the @nose.tools.raises(NotImplementedError)


Alberto Valverde

unread,
Aug 7, 2006, 11:17:19 PM8/7/06
to turbo...@googlegroups.com
Maybe the attached hack helps. Just make sure you don't keep it inside the path nosetests scans or it will spew a funky TypeError when trying to load the module when looking for tests in it. 

To use:

from TestCaseMod import *

test_foo():
failUnlessRaises(NotImplementedError, source.create)

Disclaimer: 
Minimally tested (sic)

HTH,
Alberto
TestCaseMod.py.gz

Jorge Vargas

unread,
Aug 7, 2006, 11:39:13 PM8/7/06
to turbo...@googlegroups.com
On 8/7/06, Alberto Valverde <alb...@toscat.net> wrote:


Maybe the attached hack helps. Just make sure you don't keep it inside the path nosetests scans or it will spew a funky TypeError when trying to load the module when looking for tests in it. 

To use:

from TestCaseMod import *

test_foo():
failUnlessRaises(NotImplementedError, source.create )

Disclaimer: 
Minimally tested (sic)

that's a clever hack,I didn't knew that could be done.
but did you miss the email where I talk about the nose.tools.raises ?  they have most of TestCase methods implemented there was decorators. So I can write something like this in nose.

    @raises(NotImplementedError)
    def test_create(self):
        self.sourceManager.create ()

although this is a new feature so it's nose 0.9 or above.

HTH,
Alberto




Reply all
Reply to author
Forward
0 new messages