Interaction btw unittest.assertRaises and __getattr__. Bug?

Chris Torek nospam at torek.net
Wed Oct 27 03:15:32 EDT 2010


In article <c38a5bb4-6087-453c-8873-f193927fd464 at d8g2000yqf.googlegroups.com>
Inyeol <inyeol.lee at gmail.com> wrote:
[snippage below]
>import unittest
>class C():
>    def __getattr__(self, name):
>        raise AttributeError
>class Test(unittest.TestCase):
>    def test_getattr(self):
>        c = C()
>        self.assertRaises(AttributeError, c.foo)
>unittest.main()
>-----------------------------------------------------
>... or am I missing something obvious?

As Benjamin Peterson noted, the error occurs "too soon", so that
the unittest code never has a chance to see it.

The "something obvious" is to defer the evaluation just long enough:

        self.assertRaises(AttributeError, lambda: c.foo)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html



More information about the Python-list mailing list