[Python-Dev] unittest missing assertNotRaises

Steven D'Aprano steve at pearwood.info
Wed Sep 28 01:43:13 CEST 2011


Oleg Broytman wrote:
> On Tue, Sep 27, 2011 at 07:46:52PM +0100, Wilfred Hughes wrote:
>> +    def assertNotRaises(self, excClass, callableObj=None, *args, **kwargs):
>> +        """Fail if an exception of class excClass is thrown by
>> +        callableObj when invoked with arguments args and keyword
>> +        arguments kwargs.
>> +        
>> +        """
>> +        try:
>> +            callableObj(*args, **kwargs)
>> +        except excClass:
>> +            raise self.failureException("%s was raised" % excClass)
>> +            
>> +
> 
>    What if I want to assert my test raises neither OSError nor IOError?

Passing (OSError, IOError) as excClass should do it.


But I can't see this being a useful test. As written, exceptions are 
still treated as errors, except for excClass, which is treated as a test 
failure. I can't see the use-case for that. assertRaises is useful:

"IOError is allowed, but any other exception is a bug."

makes perfect sense. assertNotRaises doesn't seem sensible or useful to me:

"IOError is a failed test, but any other exception is a bug."

What's the point? When would you use that?



-- 
Steven


More information about the Python-Dev mailing list