Opened 15 years ago

Last modified 15 years ago

#2690 defect new

assertRaises can accept a sequence of exceptions as first parameter, but errors if assertion fails

Reported by: Jonathan Lange Owned by:
Priority: normal Milestone:
Component: trial Keywords:
Cc: Branch:
Author:

Description

assertRaises((ExceptionOne, ExceptionTwo), f, *a, **kw) will succeed without error if f(*a, **kw) raises ExceptionOne or ExceptionTwo. However, it will raise an error if f does not raise one of those two exception. The error should be an AssertionError, but instead it's an obscure error based on the implementation of assertRaises itself:

File "twisted/trial/unittest.py", line 112, in failUnlessRaises
    raise self.failureException('%s not raised (%r returned)'
AttributeError: 'tuple' object has no attribute '__name__'

Change History (4)

comment:1 Changed 15 years ago by Jean-Paul Calderone

It doesn't accept a sequence. It accepts an arbitrarily deeply nested tuple. ;)

I think this sounds more like an enhancement request than a defect. It only half works by accident, not design.

comment:2 Changed 15 years ago by Jonathan Lange

I don't think it matters whether this ticket is marked as an enhancement or a defect.

However, this is a case where one of our assert methods is more broken and less useful than one of the stdlib assert methods. I think that counts as a regression.

comment:3 Changed 15 years ago by Jonathan Lange

Basic patch to make development bearable:

=== modified file 'twisted/trial/unittest.py'
--- twisted/trial/unittest.py   2007-03-09 01:53:09 +0000
+++ twisted/trial/unittest.py   2007-06-05 03:04:45 +0000
@@ -104,13 +104,15 @@
         except exception, inst:
             return inst
         except:
+            name = getattr(exception, '__name__', str(exception))
             raise self.failureException('%s raised instead of %s:\n %s'
                                         % (sys.exc_info()[0],
-                                           exception.__name__,
+                                           name,
                                            failure.Failure().getTraceback()))
         else:
+            name = getattr(exception, '__name__', str(exception))
             raise self.failureException('%s not raised (%r returned)'
-                                        % (exception.__name__, result))
+                                        % (name, result))
     assertRaises = failUnlessRaises
 
     def failUnlessEqual(self, first, second, msg=None):

comment:4 Changed 11 years ago by <automation>

Owner: Jonathan Lange deleted
Note: See TracTickets for help on using tickets.