Interaction btw unittest.assertRaises and __getattr__. Bug?

Discussion in 'Python' started by Inyeol, Oct 26, 2010.

  1. Inyeol

    Inyeol Guest

    Unittest assertRaises cannot handle exception raised inside
    __getattr__ method. Is it a bug? or am I missing something obvious?

    Here is a sample of this problem:

    -------------------------------------------------------------
    import unittest

    class C():

    def simple_attr(self):
    raise AttributeError

    def __getattr__(self, name):
    raise AttributeError

    class Test(unittest.TestCase):

    def test_simple_attr(self):
    c = C()
    self.assertRaises(AttributeError, c.simple_attr)

    def test_getattr(self):
    c = C()
    self.assertRaises(AttributeError, c.foo)

    unittest.main()
    -----------------------------------------------------

    Unittest assertRaises handles simple attribute lookup but the
    exception inside __getattr__ bypasses unittest and generates
    traceback:
    ------------------------------------------------------
    E.
    ======================================================================
    ERROR: test_getattr (__main__.Test)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
    File "xxx.py", line 19, in test_getattr
    self.assertRaises(AttributeError, c.foo)
    File "xxx.py", line 9, in __getattr__
    raise AttributeError
    AttributeError

    ----------------------------------------------------------------------
    Ran 2 tests in 0.000s

    FAILED (errors=1)
    --------------------------------------------------------------------------------------------------


    It doesn't matter what kind of exception it raises. Any exception
    inside __getattr__ bypasses assertRaises.
    This happens both with 3.1.2 and 2.6.5. Any idea?

    Inyeol
     
    Inyeol, Oct 26, 2010
    #1
    1. Advertising

  2. Re: Interaction btw unittest.assertRaises and__getattr__. Bug?

    Inyeol <inyeol.lee <at> gmail.com> writes:

    >
    > or am I missing something obvious?


    The attribute access is evaluated before the call to assertRaises, so unittest
    never has a cache to cache it.
     
    Benjamin Peterson, Oct 27, 2010
    #2
    1. Advertising

  3. Inyeol

    Chris Rebert Guest

    On Tue, Oct 26, 2010 at 7:51 PM, Benjamin Peterson <> wrote:
    > Inyeol <inyeol.lee <at> gmail.com> writes:
    >>
    >> or am I missing something obvious?

    >
    > The attribute access is evaluated before the call to assertRaises, so unittest
    > never has a
    > cache to cache it.


    or rather, "chance to catch it."

    Seems there were 2 typos.

    Cheers,
    Chris
     
    Chris Rebert, Oct 27, 2010
    #3
  4. Inyeol

    Chris Torek Guest

    In article <>
    Inyeol <> 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
     
    Chris Torek, Oct 27, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christos TZOTZIOY Georgiou

    ...And a happy new year btw (NT :)

    Christos TZOTZIOY Georgiou, Dec 31, 2003, in forum: Python
    Replies:
    0
    Views:
    289
    Christos TZOTZIOY Georgiou
    Dec 31, 2003
  2. john
    Replies:
    2
    Views:
    2,032
  3. Paul  Moore
    Replies:
    1
    Views:
    403
    Paul Moore
    Oct 14, 2008
  4. Steven D'Aprano

    Case study: debugging failed assertRaises bug

    Steven D'Aprano, Apr 26, 2011, in forum: Python
    Replies:
    2
    Views:
    344
    R David Murray
    Apr 27, 2011
  5. Ulrich Eckhardt
    Replies:
    20
    Views:
    844
    Terry Reedy
    Apr 2, 2012
Loading...

Share This Page