unittest assertRaises does not catch NoResultFound

199 views
Skip to first unread message

pr64

unread,
Jan 11, 2011, 4:56:05 AM1/11/11
to sqlalchemy
Hi,

New with sqlalchemy, i'm trying to unittest my code:

import unittest
import os
from sqlalchemy.orm.exc import NoResultFound
import User

SQLITE_FILE = 'test.db'

class UserAddTestCase(unittest.TestCase):

def setUp(self):
... some code which create the session (removed to be clearer)
self.session is stored

def tearDown(self):
if os.path.exists(self.testfile ):
os.remove(self.testfile )

def test_add_user(self):
user1 = User('user1_fn', 'user1_sn')
self.session.add(user1)
self.assertEqual(self.session.query(User).count(), 1)

self.assertEqual(self.session.query(User).filter(User.firstname ==
"user1_fn").count(), 1)
self.assertRaises(NoResultFound,
self.session.query(User).filter(User.firstname == "other_fn").one())

if __name__ == '__main__':
unittest.main()

The output is:

======================================================================
ERROR: test_add_user (__main__.UserAddTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "models.py", line 30, in test_add_user
self.assertRaises(NoResultFound,
self.session.query(User).filter(User.firstname == "other_fn").one())
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.5-
py2.6.egg/sqlalchemy/orm/query.py", line 1651, in one
raise orm_exc.NoResultFound("No row was found for one()")
NoResultFound: No row was found for one()

----------------------------------------------------------------------
Ran 1 test in 0.234s

FAILED (errors=1)

My code is supposed to raise a NoResultFound exception which is ok but
the assertRaises does not catch it and my test fails but should be a
success...

Anyone could explain me ?

thanks a lot

Michael Bayer

unread,
Jan 11, 2011, 10:26:25 AM1/11/11
to sqlal...@googlegroups.com

On Jan 11, 2011, at 4:56 AM, pr64 wrote:

>
> self.assertEqual(self.session.query(User).filter(User.firstname ==
> "user1_fn").count(), 1)
> self.assertRaises(NoResultFound,
> self.session.query(User).filter(User.firstname == "other_fn").one())
>

assertRaises receives a callable, which when called raises the expected error. So you need to not actually invoke one():

self.assertRaises(NoResultFound,
self.session.query(User).
filter(User.firstname == "other_fn").
one
)

> if __name__ == '__main__':
> unittest.main()
>
> The output is:
>
> ======================================================================
> ERROR: test_add_user (__main__.UserAddTestCase)
> ----------------------------------------------------------------------
> Traceback (most recent call last):
> File "models.py", line 30, in test_add_user
> self.assertRaises(NoResultFound,
> self.session.query(User).filter(User.firstname == "other_fn").one())
> File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.6.5-
> py2.6.egg/sqlalchemy/orm/query.py", line 1651, in one
> raise orm_exc.NoResultFound("No row was found for one()")
> NoResultFound: No row was found for one()
>
> ----------------------------------------------------------------------
> Ran 1 test in 0.234s
>
> FAILED (errors=1)
>
> My code is supposed to raise a NoResultFound exception which is ok but
> the assertRaises does not catch it and my test fails but should be a
> success...
>
> Anyone could explain me ?
>
> thanks a lot
>

> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>

Chris Withers

unread,
Jan 14, 2011, 9:56:39 AM1/14/11
to sqlal...@googlegroups.com, Michael Bayer
On 11/01/2011 15:26, Michael Bayer wrote:
>
> On Jan 11, 2011, at 4:56 AM, pr64 wrote:
>
>>
>> self.assertEqual(self.session.query(User).filter(User.firstname ==
>> "user1_fn").count(), 1)
>> self.assertRaises(NoResultFound,
>> self.session.query(User).filter(User.firstname == "other_fn").one())
>>
>
> assertRaises receives a callable, which when called raises the expected error. So you need to not actually invoke one():
>
> self.assertRaises(NoResultFound,
> self.session.query(User).
> filter(User.firstname == "other_fn").
> one
> )

Yeah, I find this pattern a bit clunky. Nowadays, context managers
provide a much nicer way of doing these kinds of tests. Most testing
packages have some flavour of this, my testfixtures package has ShouldRaise:

http://packages.python.org/testfixtures/exceptions.html#the-shouldraise-context-manager

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk

Reply all
Reply to author
Forward
0 new messages