Comment

Jason P.

How is this any different than a test like

def testClass(self):
    x = ValueError(example)
    self.assertTrue(isinstance(x, StandardError))

Why should checking class membership have different semantics for assertRaises than it does for isinstance?

If you really need to validate that the exact class is being raised, and not a subclass (which you shouldn't ever need to do, since try...except clauses will still catch the subclass), you can use the context manager behavior of assertRaises in unittest2 or Python 2.7

with self.assertRaises(BadAssError) as cm:
      foo()

self.assertEqual(cm.exception.__class__, BadAssError)

Replies

Peter Bengtsson

Why? In the same sense that `assertEqual` does an `==` not an `isintance`.
My point is that it's a bit surprising. I'm not pointing out that it's a bug.