Interaction btw unittest.assertRaises and __getattr__. Bug?
Inyeol
inyeol.lee at gmail.com
Tue Oct 26 16:29:08 EDT 2010
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
More information about the Python-list
mailing list