>
> 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.
>
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