Interaction btw unittest.assertRaises and __getattr__. Bug?

Chris Torek nospam at
Wed Oct 27 09:15:32 CEST 2010

In article <c38a5bb4-6087-453c-8873-f193927fd464 at>
Inyeol <inyeol.lee at> 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,
>... 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:
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)

More information about the Python-list mailing list